SHARE
TWEET

A

a guest Oct 27th, 2015 126 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package cz.muni.fi.pa165.deliveryservice.dao;
  2.  
  3. import cz.muni.fi.pa165.deliveryservice.PersistenceApplicationContext;
  4. import cz.muni.fi.pa165.deliveryservice.entity.Employee;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.test.context.ContextConfiguration;
  7. import org.springframework.test.context.TestExecutionListeners;
  8. import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
  9. import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
  10. import org.springframework.transaction.annotation.Transactional;
  11. import org.testng.annotations.BeforeClass;
  12. import org.testng.annotations.Test;
  13. import javax.validation.ConstraintViolationException;
  14. import static org.junit.Assert.*;
  15.  
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import javax.persistence.PersistenceException;
  19.  
  20. import static org.testng.Assert.assertEquals;
  21.  
  22. /**
  23.  *
  24.  * @author Tomas Milota
  25.  */
  26. @ContextConfiguration(classes = PersistenceApplicationContext.class)
  27. @TestExecutionListeners(TransactionalTestExecutionListener.class)
  28. @Transactional
  29. public class EmployeeDaoTest extends AbstractTestNGSpringContextTests{
  30.    
  31.     @Autowired
  32.     private EmployeeDao employeeDao;
  33.    
  34.     private Employee courier;
  35.     private Employee accountant;
  36.        
  37.     @BeforeClass
  38.     public void setUp() {
  39.         courier = new Employee();
  40.         courier.setEmail("courier@mail.com");
  41.         courier.setFirstName("Jon");
  42.         courier.setSurname("Snow");
  43.         employeeDao.create(courier);
  44.        
  45.         accountant = new Employee();
  46.         accountant.setEmail("accountant@mail.com");
  47.         employeeDao.create(accountant);
  48.     }
  49.    
  50.     @Test
  51.     public void findAllTest() {
  52.         List<Employee> expected = new ArrayList<>();
  53.         expected.add(courier);
  54.         expected.add(accountant);
  55.        
  56.         assertEquals(expected, employeeDao.findAll());
  57.     }
  58.    
  59.     @Test
  60.     public void findByIdTest() {
  61.         assertEquals(employeeDao.findById(courier.getId()), courier);
  62.     }
  63.    
  64.     @Test
  65.     public void removeTest() {
  66.         assertNotNull(employeeDao.findById(accountant.getId()));
  67.         employeeDao.remove(accountant);
  68.         assertNull(employeeDao.findById(accountant.getId()));
  69.     }
  70.    
  71.     @Test
  72.     public void findByName() {
  73.         List<Employee> found = employeeDao.findByName("Jon Snow");
  74.         assertEquals(1, found.size());
  75.         assertEquals(courier, found.get(0));
  76.     }
  77.    
  78.     @Test
  79.     public void updateTest() {
  80.         Employee found = employeeDao.findByName("Jon Snow").get(0);
  81.         found.setSurname("Stark");
  82.         employeeDao.update(found);
  83.        
  84.         Employee found2 = employeeDao.findByName("Jon Stark").get(0);
  85.         assertEquals(found, found2);
  86.     }
  87.    
  88.     @Test(expectedExceptions = ConstraintViolationException.class)
  89.     public void nullEmailTest() {
  90.         Employee emp = new Employee();
  91.         emp.setEmail(null);
  92.         employeeDao.create(emp);               
  93.     }
  94.    
  95.     @Test(expectedExceptions=PersistenceException.class)
  96.     public void emailIsUniqueTest(){
  97.         Employee courier2 = new Employee();
  98.         courier2.setEmail("courier@mail.com");
  99.         employeeDao.create(courier2);
  100.     }
  101. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top