为什么当我通过
输出: 仅限:
Mockito
注入模拟对象时,我的 @PostConstruct
方法没有被调用?@Service
public class MyService {
public MyService() {
System.out.println("CONSTRUKTOR");
}
@PostConstruct
public void init() {
System.out.println("POST CONSTRUCT");
}
@RunWith(MockitoJUnitRunner.class)
public class Mockito1 {
@InjectMocks
private MyService service;
@Before
public void init() {
}
输出: 仅限:
CONSTRUKTOR
@PostConstruct
构造甚至不适用。那么,你使用这个模拟的目的是什么?你想测试这个服务还是想在其他地方使用你的服务模拟? - Makoto