java - 使用 Mockito 在 PersonServiceTest 中编写单元测试
问题描述
请帮助我,使用 Mockito 在 PersonServiceTest 中编写单元测试来PersonDao
模拟PersonService
.
我尝试使用 when.then 方法,但我没有理想的使用方法
PersonService 类片段
public PersonService(PersonDao personDao) {
m_PersonDao = personDao;
}
public List<Person> getAll() {
return m_PersonDao.getAll();
}
public void save(Person p) {
if (!m_PersonDao.isExistingPerson(p)) {
m_PersonDao.save(p);
}
}
public Person getByName(String name) {
return m_PersonDao.getByName(name);
}
public Person getByBirthYear(Integer year) {
return m_PersonDao.getByBirthYear(year);
}
PersonServiceTest 类片段
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testPersonService() {
fail("Not yet implemented");
}
@Test
public void testGetAll() {
fail("Not yet implemented");
}
@Test
public void testSave() {
fail("Not yet implemented");
}
@Test
public void testGetByName() {
PersonDao personDao = Mockito.mock(PersonDao.class);
Mockito.when(personDao.getByName("john")).thenReturn(new Person("John Thomspon", 1856);
Person p = personDao.getByName("john");
assert (p.getName().equals("John Thomspon"));
//create mock objects: & create Instantiates the class under test using the created mock
}
@Test
public void testGetByBirthYear() {
enter code here
}
解决方案
推荐阅读
- c++ - 删除所有左值转换运算符
- c++ - 静态函数上未解析的外部符号
- algorithm - 扫雷谜题
- django - 根据字段上的条件过滤 django 查询集?
- javascript - 如何在打字稿中有条件地设置函数参数类型?
- javascript - 在为 mySQL 查询构建工作订单时,localStorage 是一个好方法吗?
- amazon-web-services - Terraform AWS:无法将文件复制到 EC2
- javascript - Web浏览器中的Java脚本nodejs
- javascript - forEach 只执行一次 javascript
- python - 即使文件存在,加载泡菜文件也会返回“找不到具有名称的文件”(file_name)