mockito确实不能模拟CGLIB已经增强的对象吗?模拟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
的第二次原因。
如果我不能窥探enhaced对象,那么我如何在我的集成测试中测试getArticle()
方法,以便我可以验证是否返回了正确的DTO。
注:我其实不想测试文件系统调用的方法。只是数据库的。那就是为什么我需要测试getArticle
方法。
以下是我发现的[文档](http://docs.mockito.googlecode.com/hg/latest/org/mockito/Mockito.html#doNothing())我不模仿地看到问题。你是否尝试过自己创建'Article',而不是自动装配(或者至少在自动装配后验证了正确性)? – atomman
是的,如果我自己创造“文章”,我可以窥探。但我必须自动装载它,因为在我的应用程序中,每个对象都是通过自动装配创建的,如果我自己启动“Article”,则Article类中的字段为空(例如,reqHandler对象)。如果我也启动这些字段,那么这些类中的字段为空,链接继续。 – samach
过滤的代码和您的问题不适合 - getArticle()有问题,getArticleContents()在代码中 - 它造成了一些混淆。你应该考虑提供更多的'IntegrationTest'代码。 – Cebence