[10] use of the injection framework RoboGuice: (Your First Testcase) and roboguicetestcase

Source: Internet
Author: User
Tags how to write test cases

[10] use of the injection framework RoboGuice: (Your First Testcase) and roboguicetestcase

In the previous article, we briefly introduced the use of RoboGuice ([9] injection framework RoboGuice: (Your First Injected Service and BroadcastReceiver). Today, let's take a look at the injection of test cases (TestCase ).

RoboGuice makes it easier for us to implement testable Android applications. This article will describe in detail how to write test cases when testing has benefited from RoboGuice. This article uses Android Robolectric, which is suitable for most Android tests.

We use Mockito to simulate relational dependencies, and EasyMock uses the same method.

(1): To test the subclass of RoboActivity. Suppose there is an Activity using Service

public class MyRoboActivity extends RoboActivity {    @Inject MyService service;    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        service.compute();    }}public class MyService {    public void compute() {        ...    }}
Then you need to write a test case to check whether the Activity correctly calls servce.

@RunWith(RobolectricTestRunner.class)public class MyActivityTest {    private Service serviceMock = mock(Service.class);    @Before    public void setup() {        // Override the default RoboGuice module        RoboGuice.overrideApplicationInjector(Robolectric.application, new MyTestModule());    }    @After    public void teardown() {        // Don't forget to tear down our custom injector to avoid polluting other test classes        RoboGuice.Util.reset();    }    @Test    public void createTriggersCompute() throws InterruptedException {        Robolectric.buildActivity(MyActivity.class).create().start();        verify(serviceMock).compute();    }    public class MyTestModule extends AbstractModule {        @Override        protected void configure() {            bind(Service.class).toInstance(serviceMock);        }    }}
This test includes the following items:

①: When set, it overwrites the default binding of RoboGuice and uses a custom test module.

②: This test module binds the Service to mock.

③: This test creates a MyActivity instance, which obtains mock through injection.
④: Verify that mock should be called.

(2) test a service. Assume that there is a service as follows:

public class MyService {    @Inject Vibrator vibrator;    @Inject Context context;    public void compute() {        context...        vibrator.vibrate(...);    }}
Then we can write a simple test to check whether the service correctly calls vibrator.

@RunWith(RobolectricTestRunner.class)public class ServiceTest {    protected Vibrator vibratorMock = mock(Vibrator.class);    protected Service service;    @Before    public void setup() {        // Override the default RoboGuice module        RoboGuice.overrideApplicationInjector(Robolectric.application, new MyTestModule());        service = RoboGuice.getInjector(Robolectric.application).getInstance(Service.class);    }    @After    public void teardown() {        // Don't forget to tear down our custom injector to avoid polluting other test classes        RoboGuice.Util.reset();    }    @Test    public void computeShouldCausePhoneToVibrate() {        service.compute();        verify(vibratorMock).vibrate(...);    }    public class MyTestModule extends AbstractModule {        @Override        protected void configure() {            bind(Vibrator.class).toInstance(vibratorMock);        }    }}
This test includes the following items:

①: When set, it overwrites the default binding of RoboGuice and uses a custom test module.
②: This test module binds Vibrator to mock.

③: This test module will create a service instance and obtain the context and mock through injection.

④: Verify that mock has been called







Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.