Java 单元测试演示者的业务逻辑

Java 单元测试演示者的业务逻辑,java,android,unit-testing,Java,Android,Unit Testing,我正在尝试对演示者的代码进行单元测试。正如您在下面的代码中看到的,我正在提出一个改装请求,如果响应成功,我将从视图中调用一个方法 我要测试的演示者的代码: @Override public void onLoadChatrooms(String accountId, String pageNum) { getChatroomsService.getChatrooms(apiToken, createRequestBodyForGetChatroomsRequest(accountId,

我正在尝试对演示者的代码进行单元测试。正如您在下面的代码中看到的,我正在提出一个改装请求,如果响应成功,我将从视图中调用一个方法

我要测试的演示者的代码:

@Override
public void onLoadChatrooms(String accountId, String pageNum) {
    getChatroomsService.getChatrooms(apiToken, createRequestBodyForGetChatroomsRequest(accountId, pageNum))
            .enqueue(new Callback<GetChatroomsServiceResponse>() {
                @Override
                public void onResponse(Call<GetChatroomsServiceResponse> call, Response<GetChatroomsServiceResponse> response) {
                    if (response.isSuccessful()) {
                        view.showData(Arrays.asList(response.body().getChatRoomsArray()));
                    }
                }

                @Override
                public void onFailure(Call<GetChatroomsServiceResponse> call, Throwable t) {

                }
            });
}
其中,ChatPresenter的第40行为:。将新回调排入队列{

有人可以帮忙吗?我试着检查Presenter是否为null,这不是问题所在

编辑:

ChatPresenter的构造函数:

class ChatPresenter implements ChatMVP.Presenter {

private ChatMVP.View view;
private GetChatroomsService getChatroomsService;
private String apiToken;

@Inject
ChatPresenter(ChatMVP.View view, GetChatroomsService getChatroomsService, @Named("Api-Token") String apiToken) {
    this.view = view;
    this.getChatroomsService = getChatroomsService;
    this.apiToken = apiToken;
}
和GetChatroomsService:

interface GetChatroomsService {

@POST("getchatrooms")
Call<GetChatroomsServiceResponse> getChatrooms(@Query("api_token") String apiToken, @Body RequestBody requestBody);

}

这里的问题是getChatroomsService中的模拟方法getChatrooms返回null。最可能的原因是生产代码中给出的参数与模拟配置中的参数不匹配

我自己在配置mock时使用any*matcher,并明确地验证生产代码传入的参数,这样可以避免像这样的非描述性NPE

@Test
public void onLoadChatrooms() throws Exception {
    when(getChatroomsService.getChatrooms(anyString(), any(RequestBody.class)))
            .thenReturn(call);

    chatPresenter.onLoadChatrooms(accountId, "0");

    verify(call).enqueue(callback.capture());
    callback.getValue().onResponse(call, getResponse());

    verify(getChatroomsService).getChatrooms(apiToken,requestBody);
    verify(view).showData(chatroomsResponseNestedItems);
}

getChatroomsService是一个变量还是你错过了括号?ChatPresenterlook的构造函数是什么样子的?@TimothyTruckle我已经用ChatPresenter构造函数的代码更新了这个问题-getChatroomsService也是一个变量请帮你自己和你的同事一个忙,将getChatroomsService重命名为complie感谢您指出这一点。@TimothyTruckle!您是对的,这是一个非常糟糕的名字。我会纠正它。哇,这是一个很好的发现!!非常感谢!我刚刚开始进行单元测试,对any*的使用我仍然不清楚。例如,在调用onLoadChatrooms时,我尝试为accountId和pageNum vars添加任何字符串,但我成功了正在查找一些我现在记不起来的错误。@Mes:哇,这是一个很好的发现!!非常感谢!我刚开始进行单元测试,我在开始时遇到了相同的问题…;o-我在添加任何字符串之前尝试过[…]但是我遇到了一些我现在记不起来的错误。Mockito希望你严格要求,要么使用匹配器,要么使用模拟对象,你不能混入参数列表。
interface GetChatroomsService {

@POST("getchatrooms")
Call<GetChatroomsServiceResponse> getChatrooms(@Query("api_token") String apiToken, @Body RequestBody requestBody);

}
@Test
public void onLoadChatrooms() throws Exception {
    when(getChatroomsService.getChatrooms(anyString(), any(RequestBody.class)))
            .thenReturn(call);

    chatPresenter.onLoadChatrooms(accountId, "0");

    verify(call).enqueue(callback.capture());
    callback.getValue().onResponse(call, getResponse());

    verify(getChatroomsService).getChatrooms(apiToken,requestBody);
    verify(view).showData(chatroomsResponseNestedItems);
}