mockito 单元测试需要但未调用:
mockito unit testing Wanted but not invoked:
我看到 SO 中已经存在类似的问题,我尝试了所有解决方案,但无法解决我的问题,因为我是 tdd
的新手
我有一个class这样的
public class AppUpdatesPresenter {
public void stopService() {
ServiceManager.on().stopService();
}
}
我有这样的测试class
@RunWith(MockitoJUnitRunner.class)
public class AppUpdatesPresenterTest {
@Mock
AppUpdatesPresenter appUpdatesPresenter;
@Mock
ServiceManager serviceManager;
@Mock
Context context;
@Test
public void test_Stop_Service() throws Exception {
appUpdatesPresenter.stopService();
verify(serviceManager,times(1)).stopService();
}
}
当我尝试测试时,如果我调用 stopService()
方法,那么 ServiceManager.on().stopService();
至少调用一次。
但我收到以下错误
Wanted but not invoked:
serviceManager.stopService();
-> at io.example.myapp.ui.app_updates.AppUpdatesPresenterTest.test_Stop_Service(AppUpdatesPresenterTest.java:103)
Actually, there were zero interactions with this mock.
不确定出了什么问题。
当您调用 appUpdatesPresenter.stopService();
时,没有任何反应,因为您没有告诉它应该发生什么。
为了让你的测试通过,你需要存根appUpdatesPresenter
。
@Test
public void test_Stop_Service() throws Exception {
doAnswer { serviceManager.stopService(); }.when(appUpdatesPresenter).stopService()
appUpdatesPresenter.stopService();
verify(serviceManager).stopService();
}
顺便说一句,上面的测试毫无意义,因为你把所有的东西都存根了。
为了使测试用例有意义,您应该注入 ServiceManager
而不是将其与 AppUpdatePresenter
耦合。
public class AppUpdatesPresenter {
private final ServiceManager serviceManager;
public AppUpdatesPresenter(ServiceManager serviceManager) {
this.serviceManager = serviceManager;
}
public void stopService() {
sm.stopService();
}
}
然后把AppUpdatesPresenter
设为待测
@InjectMock AppUpdatesPresenter appUpdatesPresenter;
现在测试用例不依赖固定交互,而是代码的实际实现。
@Test
public void test_Stop_Service() throws Exception {
appUpdatesPresenter.stopService();
verify(serviceManager).stopService();
}
我看到 SO 中已经存在类似的问题,我尝试了所有解决方案,但无法解决我的问题,因为我是 tdd
我有一个class这样的
public class AppUpdatesPresenter {
public void stopService() {
ServiceManager.on().stopService();
}
}
我有这样的测试class
@RunWith(MockitoJUnitRunner.class)
public class AppUpdatesPresenterTest {
@Mock
AppUpdatesPresenter appUpdatesPresenter;
@Mock
ServiceManager serviceManager;
@Mock
Context context;
@Test
public void test_Stop_Service() throws Exception {
appUpdatesPresenter.stopService();
verify(serviceManager,times(1)).stopService();
}
}
当我尝试测试时,如果我调用 stopService()
方法,那么 ServiceManager.on().stopService();
至少调用一次。
但我收到以下错误
Wanted but not invoked:
serviceManager.stopService();
-> at io.example.myapp.ui.app_updates.AppUpdatesPresenterTest.test_Stop_Service(AppUpdatesPresenterTest.java:103)
Actually, there were zero interactions with this mock.
不确定出了什么问题。
当您调用 appUpdatesPresenter.stopService();
时,没有任何反应,因为您没有告诉它应该发生什么。
为了让你的测试通过,你需要存根appUpdatesPresenter
。
@Test
public void test_Stop_Service() throws Exception {
doAnswer { serviceManager.stopService(); }.when(appUpdatesPresenter).stopService()
appUpdatesPresenter.stopService();
verify(serviceManager).stopService();
}
顺便说一句,上面的测试毫无意义,因为你把所有的东西都存根了。
为了使测试用例有意义,您应该注入 ServiceManager
而不是将其与 AppUpdatePresenter
耦合。
public class AppUpdatesPresenter {
private final ServiceManager serviceManager;
public AppUpdatesPresenter(ServiceManager serviceManager) {
this.serviceManager = serviceManager;
}
public void stopService() {
sm.stopService();
}
}
然后把AppUpdatesPresenter
设为待测
@InjectMock AppUpdatesPresenter appUpdatesPresenter;
现在测试用例不依赖固定交互,而是代码的实际实现。
@Test
public void test_Stop_Service() throws Exception {
appUpdatesPresenter.stopService();
verify(serviceManager).stopService();
}