【发布时间】:2017-04-19 19:42:04
【问题描述】:
我正在为以下代码编写单元测试,即使我在模拟这些字段,我也得到了 NPE,我该如何解决这个问题。这些字段带有@Inject 注释
@Component
interface A {
void run();
}
class B {
@Inject
A a;
void someMethod() {
a.run();
}}
class C{
@Inject
B b;
void anotherMethod() {
b.someMethod();
}
}
class CTest {
@Mock
B b;
// remains null when invoked in the actual class though its mocked instance is
// present here
@Mock
A a;
//// remains null when invoked in the actual class though its mocked instance
//// is present here
@InjectMocks
C c;
@Before
public void initialize() {
MockitoAnnotations.initMocks(this);
}
@Test
public void test() {
c.anotherMethod();
}
}
那么如何在使用 Mockito 向字段注入 @Inject 的实际类中获取模拟值?
【问题讨论】:
标签: java unit-testing junit mockito inject