Android测试-Robolectric Mockito Raterrofit被通缉但没有调用错误



试图使用robolectric和overito在我的android应用中测试我的改造呼叫,但我会收到以下错误:

想要但不调用:mockapi.register( ,,,, ); ->在servicetest.testapi(servicetest.java:58)实际上,与此模拟的相互作用为零。

改装API调用在接口中定义如下:

@FormUrlEncoded
@POST("/register")
void register(
        @FieldMap Map<String, String> registrationParams,
        Callback<JsonObject> response) ;

我的测试类如下:

@Config(constants = BuildConfig.class)
@RunWith(TestRunner.class)
public class SharedServiceTest {
private RegistrationActivity activity;
@Mock
private SharedService mockApi;
@Captor
private ArgumentCaptor<Callback<JsonObject>> cb;

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    ActivityController<RegistrationActivity> controller = Robolectric.buildActivity(RegistrationActivity.class);
    activity = controller.get();
    controller.create();
}
@Test
public void testAPI() throws Exception {
    activity.populateFields();
    activity.validateFields();
    activity.register("");
    Mockito.verify(mockApi).register(Mockito.anyMap(), cb.capture());
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("sessionToken", Mockito.anyString());
    jsonObject.addProperty("userId", Mockito.anyString());
    cb.getValue().success(jsonObject, null);
    Assert.assertTrue(ShadowToast.getTextOfLatestToast().contains("Registration completed"));
}
}

使用API的我的注册行为中的方法如下:

public void register(){
       MyApplication.getInstance().getSharedService().register(mRegistrationParams, new Callback<JsonObject>() {
            @Override
            public void success(JsonObject jsonObject, retrofit.client.Response response) {
                Toast.makeText(RegistrationActivity.this, "Registration completed", Toast.LENGTH_LONG).show();
            }
            @Override
            public void failure(RetrofitError error) {
                Toast.makeText(RegistrationActivity.this, RetrofitUtils.getErrorMessage(error), Toast.LENGTH_LONG).show();
            }
        });
    }

真正的改造服务来自我自己的应用程序类

public class TestMyApplication extends MyApplication
        implements TestLifecycleApplication {
    @Override
    public void onCreate() {
        super.onCreate();
    }
    @Override public void beforeTest(Method method) {
    }
    @Override public void prepareTest(Object test) {
    }
    @Override public void afterTest(Method method) {
    }
    @Override public CPSharedService getCPSharedService() {
        return Mockito.mock(SharedService.class);
    }
}

我已经搜索了其他问题,以便有此错误,但它们都不匹配我在这里要做的事情或为问题提供解决方案,所以我只是想知道我在做错了什么?

TestMyApplicationSharedService的模拟实例与您声明的测试类别不同。Mockito.verify(mockApi).register(Mockito.anyMap(), cb.capture());失败了,因为实际上从未调用过mockApi字段引用的实例。另一个问题是TestMyApplication中的GETTER总是为每个援引返回一个新的模拟:

    @Override public CPSharedService getCPSharedService() {
        return Mockito.mock(SharedService.class); //this creates a new "mocked" instance 
    }

您的场景对我而言并不清楚,但是如果您可以让测试设置testmyapplication实例中的Mockapi字段的实例,那就更好了:

public class TestMyApplication extends MyApplication
        implements TestLifecycleApplication {
    private SharedService sharedService;
    @Override
    public void onCreate() {
        super.onCreate();
    }
    @Override public void beforeTest(Method method) {
    }
    @Override public void prepareTest(Object test) {
    }
    @Override public void afterTest(Method method) {
    }
    @Override public CPSharedService getCPSharedService() {
        return this.sharedService;
    }
    public void setCPSharedService(SharedService sharedService) {
        // store your mock
        this.sharedService = sharedService;
    }
}

在您的测试课中:

@Test
public void testAPI() throws Exception  {
    // configure you TestMyApplication
    assertTrue(MyApplication.getInstance() instanceof TestMyApplication);
    TestMyApplication testMyApp = (TestMyApplication) MyApplication.getInstance();
    testMyApp.setCPSharedService(this.mockApi);
    activity.populateFields();
    activity.validateFields();
    activity.register("");
    Mockito.verify(this.mockApi).register(Mockito.anyMap(), cb.capture());
    ...
}

相关内容

  • 没有找到相关文章

最新更新