모티토를 사용하여 클래스의 메서드를 테스트하고 싶습니다.모의 메서드가 예상 값을 반환하지 않습니다.
public class SplashPresenter {
public volatile State mField1 = State.DEFAULT;
public volatile State mField2 = State.DEFAULT;
boolean stateFlagsAreAllCompleted(@NonNull final ISplashView view) {
if (mField1 == State.COMPLETE //
&& mField2 == State.COMPLETE) {
// Check Forced Update
final int updateCheckResult = checkForcedUpdate(); // <===
if (updateCheckResult == MyConstants.PRODUCTION_UPDATE_AVAILABLE) {
view.displayForcedUpdateAlert(false);
return true;
}
if (updateCheckResult == MyConstants.BETA_UPDATE_AVAILABLE) {
view.displayForcedUpdateAlert(true);
return true;
}
view.restartLoader();
// Move to the home screen
return true;
}
return false;
}
int checkForcedUpdate() {
...... // my codes
}
}
이 내 테스트 클래스입니다 :
public class SplashPresenterTest_ForStateFlags {
private Context mContext;
private ISplashView mView;
@Before
public void setUp() throws Exception {
mContext = Mockito.mock(Context.class);
mView = Mockito.mock(ISplashView.class);
}
@Test
public void stateFlagsAreAllCompleted() throws Exception {
SplashPresenter presenter = Mockito.mock(SplashPresenter.class);
presenter.mField1 = State.COMPLETE;
presenter.mField2 = State.COMPLETE;
when(presenter.checkForcedUpdate()).thenReturn(1);
boolean actual = presenter.stateFlagsAreAllCompleted(mView);
System.out.println("actual: " + actual + ", " +
presenter.mField1 + ", " +
presenter.checkForcedUpdate());
assertTrue(actual);
}
}
테스트 실패가 끝에서 일어나는 것이다 :(이것은 출력 :
실제 : 거짓, COMPLETE 1
내가 이해할 수없는 것은 심지어 stateFlagsAreAllCompleted
메서드를 foll로 변경하는 것입니다. 코드가 있기 때문에 여전히 위의 출력으로 테스트가 실패합니다.
boolean stateFlagsAreAllCompleted(@NonNull final ISplashView view) {
return true;
}
그래서 어떤 권장 사항도 인정 될 것입니다. 감사.
실제로 (메서드 내에서 설계된) 다른 조건에 따라'stateFlagsAreAllComplete()'의 출력을 테스트하고 싶습니다. 그래서 이렇게하면 출력을 평가할 수 없습니다. – Hesam
지금 문제를보고 있습니다. 테스트중인 메소드의 클래스를 조롱하고 있습니다. 테스트중인 클래스를 조롱하면 안됩니다 (테스트 목적을 무효화). 의존성만을 조롱해야합니다. 클래스의 인스턴스를 만든 다음 테스트합니다. 나는 내 대답 – ucsunil
아를 업데이트, 나는 몰랐다. 귀하의 답변에 감사드립니다. – Hesam