Mockito - Wanted but not invoked: Actually, there were zero interactions with this mock


要測試的類:IndexController.java

代碼:

@Mock
private TemplateWrapper templateWrapper = spy(new TemplateWrapper());

@InjectMocks
private IndexController controller;

verify(templateWrapper).process();

報錯:

Wanted but not invoked:
templateWrapper.process();
-> at com.suning.ebuy.cms.web.controller.IndexControllerTest.testIndexSuccess(IndexControllerTest.java:199)
Actually, there were zero interactions with this mock.

錯誤原因:沒有把templateWrapper注入到IndexController.java類中.

解決方法:

1.把templateWrapper注入到IndexController.java類中

controller.setMyObj(templateWrapper); //it is good to have in @before method

2.或者創建templateWrapper的實例類,再執行verify(templateWrapper).process();

templateWrapper = new TemplateWrapper();

verify(templateWrapper).process();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM