[英]Mockito. How to mock abstract method call in class under test?
I have Test class for controller. 我有控制器的测试类。 I need to inject autowired field. 我需要注入自动接线字段。 And mock method of Controller's super class (I have no possibility to refactor this situation) 和Controller的超类的模拟方法(我无法重构这种情况)
public class ControllerTest {
@Mock
Service service;
@InjectMocks
Controller controller;
@Test
public vois test() {
controller.process();
}
}
In process() method service is called and superclass's method, too. 在process()中,方法服务也被调用,而超类的方法也被调用。
I tried to init my controller in test in this way 我试图以此方式初始化我的控制器
new Controller() {
@Override
superMethod() {
//do nothing
}
}
But I cannot inject service mock in this case due to setter is missing. 但是由于缺少setter,在这种情况下我无法注入服务模拟。 I can do it with reflection, but maybe someone knows more elegant solution? 我可以反射一下,但是也许有人知道更优雅的解决方案?
If you are using annotations, you must to put @RunWith(MockitoJUnitRunner.class) above the ControllerTest or create setUp method like this: 如果使用批注,则必须将@RunWith(MockitoJUnitRunner.class)放在ControllerTest上方,或创建如下的setUp方法:
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
Hope this help. 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.