我正在使用 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);
}
}