publicbooleanupdate(int id, String name){ Person person = personDao.getPerson(id); if (person == null) { returnfalse; } Person personUpdate = new Person(); personUpdate.setId(person.getId()); personUpdate.setName(name); return personDao.update(personUpdate); }
publicbooleangetData(int id)throws Exception { Person person = personDao.getPerson(id); if (person == null) { new Exception("报错了!!!"); } returntrue; } }
dao层
1 2 3 4 5 6
publicinterfacePersonDao{ Person getPerson(int id);
@InjectMocks private PersonService personService = new PersonService();
@Mock private PersonDao personDao;
@Before publicvoidbeforeUpdate(){ Person person = new Person(); person.setId(1); person.setName("chulei"); when(personDao.getPerson(1)).thenReturn(person); //when().thenThrow(Exception.class); when(personDao.update(person)).thenReturn(true); }