集成测试

集成测试主要针对不直接用用户交互的组件,比如service和contentprovider。

测试 service

  1. RunWith(AndroidJUnit4.class)并且指定AndroidJUnitRunner
  2. 创建ServiceTestRule通过@Rule注释
  3. 编写测试方法

    @Test
    public void testWithBoundService() throws TimeoutException {
        // Create the service Intent.
        Intent serviceIntent =
                new Intent(InstrumentationRegistry.getTargetContext(),
                    LocalService.class);
    
        // Data can be passed to the service via the Intent.
        serviceIntent.putExtra(LocalService.SEED_KEY, 42L);
    
        // Bind the service and grab a reference to the binder.
        IBinder binder = mServiceRule.bindService(serviceIntent);
    
        // Get the reference to the service, or you can call
        // public methods on the binder directly.
        LocalService service =
                ((LocalService.LocalBinder) binder).getService();
    
        // Verify that the service is working correctly.
        assertThat(service.getRandomInt(), is(any(Integer.class)));
    }
    

    测试 contentprovider