使用Mockito在PersonServiceTest中编写单元测试

时间:2019-01-23 10:27:03

标签: java junit mockito

请帮助我,使用Mockito在PersonDao中模拟PersonService对象,在PersonServiceTest中编写单元测试。

我试图使用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
}

0 个答案:

没有答案