Mockito 将 Mock 注入间谍对象
2022-09-01 00:46:29
我正在为一个具有2级依赖注入的类编写一个测试用例。我对1级依赖注入对象使用@Spy注释,并且我想模拟第2级注入。但是,我一直在第二级上获得空指针异常。有没有办法将模拟注入@Spy对象?
public class CarTestCase{
@Mock
private Configuration configuration;
@Spy
private Engine engine;
@InjectMocks
private Car car;
@Test
public void test(){
Mockito.when(configuration.getProperties("")).return("Something");
car.drive();
}
}
public class Car{
@Inject
private Engine engine;
public void drive(){
engine.start();
}
}
public class Engine{
@Inject
private Configuration configuration;
public void start(){
configuration.getProperties(); // null pointer exception
}
}