【发布时间】:2017-11-24 02:35:05
【问题描述】:
当我使用@InjectMocks 时,发生了异常。我的代码如下所示:
class A {
private X x;
private Y y;
public A(String ip, int port) {
this(someMethodCall(ip, port)); //
}
private A(X x) {
this.x = x;
this.y = new Y();
}
}
UT:
public class ATest() {
@InjectMocks A a;
@Mock X x;
@Mock Y y;
@Test ...
}
它会抛出一个 NPE,有人可以帮助我吗?
org.mockito.exceptions.base.MockitoException: Cannot instantiate @InjectMocks field named 'channel' of type 'class Juinit3.Channel'. You haven't provided the instance at field declaration so I tried to construct the instance. However, the constructor or the initialization block threw an exception: null.
【问题讨论】:
标签: java unit-testing junit mockito