【发布时间】:2017-06-07 09:17:22
【问题描述】:
当我运行测试用例时,我得到了 AnObj 的嘲笑。这是从目标类方法内部使用的。当该方法被调用时,“anOtherObj”被访问并且被发现为空。有人可以指出如何确保“anOtherObj”不为空,这样我就不会在那里得到空指针吗?
@Test
public class TestTargetTest {
@Mock
private AnObj anObj;
@InjectMocks
private TestTarget testTarget;
@BeforeMethod
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void testTarget() {
when(anObj.someMethod()).thenCallRealMethod();
testTarget.testTarget();
}
}
@Component
public class TestTarget {
@Autowired
private AnObj anObj;
public void testTarget(){
anObj.someMethod();
}
}
@Component
public class AnObj {
@Autowired
private AnOtherObj anOtherObj;
public void someMethod(){
syso(anOtherObj.toString());
}
}
【问题讨论】:
-
什么是
mirrorIbpCusUserRepository? -
这将确保“anOtherObj.toString()”被成功调用。