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