私は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)
line 40
ChatPresenterためには、次のとおりです。.enqueue(new Callback<GetChatroomsServiceResponse>() {
誰もがそれを助けることができますか? Presenterがnullで、それが問題ではないかどうかを調べました。
EDIT:
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'変数か、かっこが足りませんでしたか?' ChatPresenter'のコンストラクタはどのように見えますか? –
@TimothyTruckle私はChatPresenterのコンストラクタのコードで質問を更新しました。また、 'getChatroomsService'は変数です – Mes
あなた自身とあなたの同僚に好意を持ち、' Java命名規約 '(http: //www.oracle.com/technetwork/java/codeconventions-135099.html)。 –