junit - 模拟单元测试需要但未调用:
问题描述
我已经看到 SO 中已经存在类似的问题,我尝试了所有解决方案,但无法解决我的问题,因为我是新手tdd
我有这样的课
public class AppUpdatesPresenter {
public void stopService() {
ServiceManager.on().stopService();
}
}
我有这样的测试课
@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();
}
推荐阅读
- authentication - remote-server authorized_keys 已经有我的 macOS 的 id_rsa.pub,但是没有密码仍然无法登录
- forms - 屏幕锁定时不显示 PowerShell 表单
- javascript - JSON.parse 不转换字符串化 JSON 数组
- javascript - fetch api 是否发生在主线程之外?如果发生在主线程上怎么不阻塞?
- html - 在火狐中,img是子元素,高度设置为100%,但是父元素的宽度是img原来的宽度
- mysql - 如何使用 SQL 查询生成测量曲线
- python - %timeit 输出因环境而异
- swift - CNContactPicker - 用户在使用搜索后无法选择联系人
- asp.net - System.Data.SqlClient.SqlException:''@Password' 附近的语法不正确。'
- javascript - Azure 认知服务 TTS:找不到资源