@BeforeClass public static void initClass() { dao = new PersonDaoImpl(); factory = mock(SessionFactory.class); session = mock(Session.class); dao.setSessionFactory(factory); }
@Test(expected = HibernateException.class) public void testDeleteDataKO() { // Setup Person input = new Person(); doThrow(new HibernateException("")).when(session).delete(input); // Action dao.deletePerson(input); }
@Test(expected = HibernateException.class) public void testRetrieveDataKO() { // /Setup Criteria criteria = mock(Criteria.class); when(session.createCriteria(Person.class)).thenReturn(criteria); when(criteria.list()).thenThrow(new HibernateException("")); dao.getAllPersons(); }
@Test(expected = HibernateException.class) public void testCreateDataKO() { // Setup Person user = new Person(); when(session.merge(user)).thenThrow(new HibernateException("fail")); // Action dao.savePerson(user); }
@Test public void testDeleteDataOK() { // Setup Person input = new Person(); // Action dao.deletePerson(input); // Test verify(factory, times(1)).getCurrentSession(); verify(session, times(1)).delete(input); }
@Test public void testCreateDataOK() { // Setup Person input = new Person(); // Action dao.savePerson(input); // test verify(factory, times(1)).getCurrentSession(); verify(session, times(1)).merge(input); }
@Test public void testRetrieveDataOK() { // /Setup Criteria criteria = mock(Criteria.class); when(session.createCriteria(Person.class)).thenReturn(criteria); List<Person> persons = mock(List.class); when(criteria.list()).thenReturn(persons); // Action dao.getAllPersons(); // Test verify(session, times(1)).createCriteria(Person.class); verify(criteria, times(1)).list(); }