단위 코드를 내 Presenter
단위 테스트하려고합니다. 코드에서 알 수 있듯이 Retrofit 요청을 만들고 있는데 응답이 성공하면 View
에서 메서드를 호출합니다.단위 테스트 발표자의 비즈니스 로직
코드 내가 테스트 할 :
@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
얻고 있다는 것입니다 : 여기
@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.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인지 확인하려고했는데 문제가 아닙니다.
편집 :
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
자신과 동료에게 'getChatroomsService'라는 이름을 [Java 명명 규칙] (http : //www.oracle.com/technetwork/java/codeconventions-135099.html). –