Написать модульный тест в PersonServiceTest с использованием Mockito - PullRequest
0 голосов
/ 23 января 2019

Пожалуйста, помогите мне, напишите Unit Test в PersonServiceTest, используя Mockito для насмешки 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
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...