![](/img/trans.png)
[英]Presenter unit test with RxJava CompositeSubscription
[英]Unit test Presenter's business logic
我正在尝试对Presenter
的代码进行单元测试。 如下面的代码所示,我正在提出一个Retrofit请求,如果响应成功,则从View
调用一个方法。
我要测试的演示者代码:
@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) {
}
});
}
这是我写的测试:
@Mock
private ChatMVP.View view;
@Mock
private GetChatroomsService getChatroomsService;
@Mock
private RequestBody requestBody;
@Mock
private Call<GetChatroomsServiceResponse> call;
@Captor
private ArgumentCaptor<Callback<GetChatroomsServiceResponse>> callback;
@Mock
private List<GetChatroomsResponseNestedItem> chatroomsResponseNestedItems;
private String accountId = "14";
private String apiToken = "someToken";
private ChatPresenter chatPresenter;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
chatPresenter = new ChatPresenter(view, getChatroomsService, apiToken);
}
@Test
public void onLoadChatrooms() throws Exception {
when(getChatroomsService.getChatrooms(apiToken, requestBody))
.thenReturn(call);
chatPresenter.onLoadChatrooms(accountId, "0");
verify(call).enqueue(callback.capture());
callback.getValue().onResponse(call, getResponse());
verify(view).showData(chatroomsResponseNestedItems);
}
问题是我正在为该行获取NPE
: chatPresenter.onLoadChatrooms(accountId, "0");
确切的错误消息是:
java.lang.NullPointerException
at my.package.main.fragments.chat.ChatPresenter.onLoadChatrooms(ChatPresenter.java:40)
at my.package.main.fragments.chat.ChatPresenterTest.onLoadChatrooms(ChatPresenterTest.java:70)
其中,ChatPresenter的line 40
行是: .enqueue(new Callback<GetChatroomsServiceResponse>() {
有人可以帮忙吗? 我尝试检查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);
}
这里的问题是getChatrooms()
中的getChatroomsService
方法getChatrooms()
返回null
。 造成这种情况的最可能原因是生产代码中给定的参数与模拟配置中的参数不匹配。
我自己在配置模拟时使用any*()
匹配器,并显式验证生产代码传递的参数,从而使我免于像此类的非描述性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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.