내부 익명 클래스가있는 메서드를 포함하는 테스트중인 클래스가 있습니다. 익명 클래스의 메서드 중 하나는 테스트 할 클래스의 메서드를 호출하지만 Mockito는이를 인식하지 못합니다. Mockito : 내부 익명 클래스의 메서드 호출 확인
public class ClassUnderTest {
Dependency dependency;
public ClassUnderTest(Dependency d) {
dependency = d;
}
public void method() {
dependency.returnsObservable().observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io()).subscribe(new Observer<SupportClass> {
/* Other methods omitted */
public void onComplete() {
outerMethod();
})
}
public void outerMethod() {
blah;
}
}
내 테스트 코드 : 내가 알아낼 수없는 이유
public class TestClass {
ClassUnderTest underTest;
Dependency dependency;
@Before
public void setUp() throws Exception {
dependency = Mockito.mock(Dependency.class);
underTest = Mockito.spy(new ClassUnderTest(dependency));
}
@Test
public void method() throws
Mockito.when(dependency.returnObservable()).thenReturn(Observable.just(new SupportClass());
Mockito.doNothing().when(underTest).outerMethod();
underTest.method();
Mockito.verify(underTest).outerMethod();
}
}
, Mockito 내가 수동으로 확인 된 경우에도, outerMethod()가 호출되고 있는지 감지 할 수 없습니다 디버거에서 한 줄씩 단계별로 진행합니다. 또한 의존성 객체에 대한 호출이 올바른 내용으로 올바른 관찰 가능을 반환하고 onComplete() 및 outerMethod() 메서드가 호출됨을 확인했습니다. 나는 Mockito가 왜 그것을 탐지하지 못하는지 혼란스러워합니다.
이것은 밖으로 뱉어하는 오류입니다 : 내가 부족 분명 아무것도
Wanted but not invoked:
classUnderTest.outerMethod();
-> at (file and line number)
However, there was exactly 1 interaction with this mock:
classUnderTest.method();
-> at (file and line number)
있습니까? 테스트 할 때 몇 가지 문제가 발생할 수 있습니다 당신은 스케줄러 사이에 변경하고
그는 '종속성'클래스를 표시합니다. 나는'innerMethod'가 호출되는 곳을보고 있지 않습니다. [MCVE] (https://stackoverflow.com/help/mcve)를 게시하는 것이 가장 좋습니다 –
일부 예제를 정리하기 위해 예제 코드를 업데이트했습니다. 종속성 클래스의 returnObservable() 메서드가 올바르게 작동하고 유효한 Observable 개체를 반환하고 onComplete() 및 outerMethod() 메서드가 호출되지만 Mockito가이를 감지하지 못하는 이유와 혼동을 느낍니다. @VinceEmigh – theasianpianist
코드가 충분하지 않습니다. 테스트 종속성은 모의이지만, 당신은'dependency.returnsObservable(). subscribe (...)'를 호출한다. dependency.returnsObservable()은 null을 반환하기 때문에 테스트에서 NPE가 발생합니다. 귀하의 모범을 보입니다. – tkruse