java - 如果未调用实体类设置器,如何在 Mockito 中进行验证
问题描述
我有一门课,我想使用 mockito 对其进行单元测试:
public class ServiceImpl {
@Autowired
private TestDao testDao;
@Transactional
public void setData(Long id) {
TestClass testClass = testDao.findOne(id);
if (testClass != null) {
testClass.setStatus(true);
}
}
}
我需要测试这个类。所以我创建了如下测试类:
@RunWith(MockitoJUnitRunner.class)
public class ServiceImplTest{
@Mock
private TestDao testDao;
@InjectMocks
private ServiceImpl service;
@Test
public void setData_success() {
TestClass testClass = new TestClass();
when(testDao.findOne(1L)).thenReturn(testClass);
service.setData(1L);
assertTrue(testClass.getStatus());
}
}
上面的场景是 testClass 对象存在的时候。同样,我想测试 testClass 对象为空时的场景。就像是:
@Test
public void setData_testClass_null() {
TestClass testClass = null;
when(testDao.findOne(1L)).thenReturn(testClass);
service.setData(1L);
//how to I verify here that testClass.setStatus(true) is never called????
//or should I just ignore the test case?
}
解决方案
推荐阅读
- python - 在python中按数字顺序打印输出
- flutter - 在 pubspec.yaml 的颤振中找不到图像文件
- tarantool - 我可以使用 mysql 或 psql 控制台连接到 Tarantool 吗?
- amazon-iam - IAM 拒绝对 SFProRole 的无服务器访问
- javascript - 根据移动语言方向更改 React Native 应用名称
- javascript - iText 7 使用 JS 脚本为 PDF 报告添加水印
- ansible - 如何访问远程主机上角色/文件下的文件
- parse-platform - 在 Parse 上安排推送通知
- python - 返回字符串中固定的数字行(数量)?
- angular - Angular 8 - 路由未发生 - 垫表行