Mockito と JUnit を使用して単体テスト ケースを作成しています。しかしNullPointerException
、テストを実行すると取得します。デバッグ時に、Mockito on method: when().thenReturn()
が依存メソッドの値を返さず、呼び出しプログラムがそれらのメソッドを呼び出して結果を取得していることを知りました。
以下は、コードの構造を理解するための私のダミーコードです。
class B {
public C getValue() {
return C;
}
}
class A {
public D getAns(String q1, String q2) {
return B.getValue().map(mapper::toD); //null pointer exception start here
}
}
@RunWith(MockitoJunitrunner.test)
class TestA {
@InjectMock
A a;
@Mock
B b;
C c;
init() {
when(b.getValue()).thenReturn(c);
}
@Test
public void getA() {
D ans=A.getAns(q1,q2); //getting null pointer exception here
AssertNotNull(ans);
}
}