使用MockitoAnnotations时未调用@PostConstruct

时间:2015-05-21 01:23:08

标签: java spring mocking mockito autowired

@RunWith(MockitoJUnitRunner.class)
public class TestMail{    
    @Autowired(required = true)
    SomeFactory someFactory;
    private @Mock MailService mailService;
    private @Captor ArgumentCaptor<List<MailList>> mailListCaptor;
    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
    }  
    @PostConstruct
    public void init() throws Exception {
            logger.info("someFactory {}", someFactory);
    }
}

这是示例代码。在使用mockito或@Beofre注释之前,一切似乎都没问题。现在它正确地模拟了对象,但someFactory没有正确自动装配。

在Mockito之前一切正常。

1 个答案:

答案 0 :(得分:0)

如果要将SomeFactory的依赖项注入其中,请使用@InjectMocks批注。

http://docs.mockito.googlecode.com/hg/1.9.5/org/mockito/InjectMocks.html

同时参考Mockito: Inject real objects into private @Autowired fields