Mockito无法mock被CGLIB增强的对象,这是真的吗?
public class Article {
@Autowired
private dbRequestHandler
@Autowired
private filesystemRequestHandler
@Transactional
public ArticleDTO getArticleContents() {
//extractText() and then save the data in DTO
//extractImages() and then save the data in DTO
// some other calls to other databases to save data in dto
return articleDTO;
}
public void extractText() {
//call to DB
}
public void extractImages() {
// call to file system
}
}
public class IntegrationTest {
@Autowired
private Article article;
//setup method {
articleMock = Mockito.spy(article);
doNothing().when(articleMock).extractImages();
}
}
在上面的示例中,当涉及到
doNothing().when(articleMock).extractImages();
时,实际上调用了真实的函数。仔细观察可以发现,articleMock被增强了两次。一次是由于autowiring
,另一次是由于spying
。如果我不能对增强对象进行间谍操作,那么我该如何在我的集成测试中测试
getArticle()
方法,以便验证返回一个正确的DTO。注意:我实际上不想测试执行文件系统调用的方法,只想测试数据库调用的方法,因此我需要测试
getArticle
方法。
Article
,而不是使用自动装配(或者至少在自动装配后验证正确性)? - atommangetArticle()
,而代码中是getArticleContents()
- 这会导致一些混淆。你应该考虑提供更多的IntegrationTest
代码。 - Cebence