@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);
    }
}


这是示例代码。在使用嘲笑或@Beofre批注之前,一切似乎都很好。现在,它可以正确模拟对象,但是someFactory不能正确自动装配。

在Mockito之前,一切正常。

最佳答案

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

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

也请参考Mockito: Inject real objects into private @Autowired fields

09-15 16:53