[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