gpt4 book ai didi

java - Android 测试 - Robolectric + Mockito + Retrofit 获取通缉但未调用错误

转载 作者:搜寻专家 更新时间:2023-11-01 09:46:08 25 4
gpt4 key购买 nike

尝试使用 Robolectric 和 Mockito 在我的 Android 应用程序中测试我的 Retrofit 调用,但我收到以下错误:

Wanted but not invoked: mockApi.register( , ); -> at ServiceTest.testAPI(ServiceTest.java:58) Actually, there were zero interactions with this mock.

RetroFit API 调用在接口(interface)中定义如下:

@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"));

}

}

我的RegistrationActivity中使用该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();
}
});

}

真正的 Retrofit 服务来 self 自己的应用程序类,我在我的测试文件夹中模拟了它以供 robolectric 使用:

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);
}
}

我已经搜索了 SO 上出现此错误的其他问题,但没有一个与我在这里尝试做的相匹配,也没有为我的问题提供解决方案,所以我只是想知道我做错了什么?

最佳答案

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

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

我不是 100% 清楚你的场景,但如果你能让你的测试在你的 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());

...

}

关于java - Android 测试 - Robolectric + Mockito + Retrofit 获取通缉但未调用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37989626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com