@Test public void testUpdateNameFail() { when(dao.fetchPerson(1)).thenReturn(null); boolean isUpdated = personService.update(1, "david"); assertFalse(isUpdated); verify(dao).fetchPerson(1); }
@Test public void testUpdateName() { Person p1 = new Person(1, "phil"); when(dao.fetchPerson(1)).thenReturn(p1); boolean isUpdated = personService.update(1, "david"); assertTrue(isUpdated); verify(dao).fetchPerson(1); verify(dao).update(any(Person.class)); // verify(dao).update(p1)会报错。因为p1对象不一样! assertEquals("david", p1.getPersonName()); // ArgumentCaptor :参数捕获器 // ArgumentCaptor<Person> personCaptor = ArgumentCaptor.forClass(Person.class); // verify(dao).update(personCaptor.capture()); // // Person updatedPerson = personCaptor.getValue(); // assertEquals("david", updatedPerson.getPersonName()); }