Java tutorial
package cz.muni.pa165.carparkapp.Service; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import cz.muni.pa165.carparkapp.DAO.BranchDAO; import cz.muni.pa165.carparkapp.DAO.CarDAO; import cz.muni.pa165.carparkapp.DAO.EmployeeDAO; import cz.muni.pa165.carparkapp.DAO.LoanDAO; import cz.muni.pa165.carparkapp.Entities.Branch; import cz.muni.pa165.carparkapp.Entities.Car; import cz.muni.pa165.carparkapp.Entities.Employee; import cz.muni.pa165.carparkapp.Entities.Loan; import cz.muni.pa165.carparkapp.dto.BranchDTO; import cz.muni.pa165.carparkapp.dto.CarDTO; import cz.muni.pa165.carparkapp.dto.EmployeeDTO; import cz.muni.pa165.carparkapp.dto.LoanDTO; import cz.muni.pa165.carparkapp.serviceImpl.BranchServiceImpl; import cz.muni.pa165.carparkapp.serviceImpl.CarServiceImpl; import cz.muni.pa165.carparkapp.serviceImpl.EmployeeServiceImpl; import cz.muni.pa165.carparkapp.serviceImpl.LoanServiceImpl; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import org.dozer.DozerBeanMapper; import org.dozer.Mapper; import org.junit.AfterClass; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; import static org.mockito.Mockito.when; import org.mockito.runners.MockitoJUnitRunner; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; /** * * @author Michal */ @RunWith(MockitoJUnitRunner.class) @ContextConfiguration(locations = { "classpath:META-INF/contextTest.xml" }) public class LoanServiceImplTest { @Autowired @InjectMocks LoanServiceImpl loanService = new LoanServiceImpl(); @Autowired @InjectMocks BranchServiceImpl branchService = new BranchServiceImpl(); @Autowired @InjectMocks CarServiceImpl carService = new CarServiceImpl(); @Autowired @InjectMocks EmployeeServiceImpl employeeService = new EmployeeServiceImpl(); @Mock LoanDAO loanDaoImplMock; @Mock CarDAO carDaoImplMock; @Mock BranchDAO branchDaoImplMock; @Mock EmployeeDAO employeeDaoImplMock; ArgumentCaptor<Loan> captorLoan = ArgumentCaptor.forClass(Loan.class); private Loan loan1; private Loan loan2; private LoanDTO loanDto1; private LoanDTO loanDto2; private Car car1; private Car car2; private CarDTO carDto1; private CarDTO carDto2; private Branch branch1; private Branch branch2; private BranchDTO branchDto1; private BranchDTO branchDto2; private Employee employee1; private Employee employee2; private EmployeeDTO employeeDto1; private EmployeeDTO employeeDto2; public LoanServiceImplTest() { } @BeforeClass public static void setUpClass() { } @AfterClass public static void tearDownClass() { } @Before public void setUp() { Calendar cal1 = Calendar.getInstance(); cal1.set(1980, Calendar.JANUARY, 1); Date date1 = cal1.getTime(); branch1 = new Branch(); branch1.setId(1); branch1.setAddress("Brno"); branch1.setCompanyNumber("123456"); branch1.setName("Pobocka 45"); branchDto1 = new BranchDTO(); branchDto1.setId(1); branchDto1.setAddress("Brno"); branchDto1.setCompanyNumber("123456"); branchDto1.setName("Pobocka 45"); branch2 = new Branch(); branch2.setId(2); branch2.setAddress("Brno"); branch2.setCompanyNumber("123456"); branch2.setName("Pobocka 45"); branchDto2 = new BranchDTO(); branchDto2.setId(2); branchDto2.setAddress("Brno"); branchDto2.setCompanyNumber("123456"); branchDto2.setName("Pobocka 45"); car1 = new Car(); car1.setRegistrationNumber("TT-588-BA"); car1.setType("Skoda"); car1.setModel("Octavia"); car1.setColor("Black"); car1.setFuel("Diesel"); car1.setVin("1415415"); car1.setId(1); car2 = new Car(); car2.setRegistrationNumber("BA-211-CJ"); car2.setType("Opel"); car2.setModel("Meriva"); car2.setColor("White"); car2.setFuel("Oil"); car2.setVin("57772332"); car2.setId(2); carDto1 = new CarDTO(1, "TT-588-BA", "Skoda", "Octavia", "1415415", "Diesel", "Black"); carDto2 = new CarDTO(2, "BA-211-CJ", "Opel", "Meriva", "57772332", "Oil", "White"); employeeDto1 = new EmployeeDTO(); employeeDto1.setId(1); employeeDto1.setFirstName("Jano"); employeeDto1.setLastName("Novak"); employeeDto1.setAddress("Botanicka 68a, 60200 Brno"); employeeDto1.setBirthNumber("821104/5478"); employeeDto1.setTelNumber("765864356"); employeeDto2 = new EmployeeDTO(); employeeDto2.setId(2); employeeDto2.setFirstName("Eva"); employeeDto2.setLastName("Novotna"); employeeDto2.setAddress("Nadrazni 18, 60300 Brno"); employeeDto2.setBirthNumber("821104/7548"); employeeDto2.setTelNumber("578687899"); employee1 = new Employee(); employee1.setFirstName("Jano"); employee1.setLastName("Novak"); employee1.setAddress("Botanicka 68a, 60200 Brno"); employee1.setBirthNumber("821104/5478"); employee1.setTelNumber("765864356"); employee2 = new Employee(); employee2.setFirstName("Eva"); employee2.setLastName("Novotna"); employee2.setAddress("Nadrazni 18, 60300 Brno"); employee2.setBirthNumber("821104/7548"); employee2.setTelNumber("578687899"); loan1 = new Loan(); loan1.setId(1); loan1.setBranch(branch1); loan1.setCar(car1); loan1.setEmployee(employee1); loan1.setStartDate(date1); loan1.setEndDate(date1); loan2 = new Loan(); loan2.setId(2); loan2.setBranch(branch2); loan2.setCar(car2); loan2.setEmployee(employee2); loan2.setStartDate(date1); loan2.setEndDate(date1); loanDto1 = new LoanDTO(); loanDto1.setId(1); loanDto1.setBranch(branchDto1); loanDto1.setCar(carDto1); loanDto1.setEmployee(employeeDto1); loanDto1.setStartDate(date1); loanDto1.setEndDate(date1); loanDto2 = new LoanDTO(); loanDto2.setId(2); loanDto2.setBranch(branchDto2); loanDto2.setCar(carDto2); loanDto2.setEmployee(employeeDto2); loanDto2.setStartDate(date1); loanDto2.setEndDate(date1); carService.addCar(carDto1); employeeService.addEmployee(employeeDto1); branchService.addBranch(branchDto1); carService.addCar(carDto2); employeeService.addEmployee(employeeDto2); branchService.addBranch(branchDto2); } private static void assertEqualsLoan(Loan loan, LoanDTO loanDto) { Mapper mapper = new DozerBeanMapper(); assertEquals(loanDto.getId(), loan.getId()); assertEquals(mapper.map(loanDto.getBranch(), Branch.class), loan.getBranch()); assertEquals(mapper.map(loanDto.getCar(), Car.class), loan.getCar()); assertEquals(mapper.map(loanDto.getEmployee(), Employee.class), loan.getEmployee()); assertEquals(loanDto.getEndDate(), loan.getEndDate()); assertEquals(loanDto.getStartDate(), loan.getStartDate()); } private static void assertEqualsLoanCaptor(LoanDTO loanDto, ArgumentCaptor<Loan> captorLoan) { Mapper mapper = new DozerBeanMapper(); assertEquals(loanDto.getId(), captorLoan.getValue().getId()); assertEquals(mapper.map(loanDto.getBranch(), Branch.class), captorLoan.getValue().getBranch()); assertEquals(mapper.map(loanDto.getCar(), Car.class), captorLoan.getValue().getCar()); assertEquals(mapper.map(loanDto.getEmployee(), Employee.class), captorLoan.getValue().getEmployee()); assertEquals(loanDto.getEndDate(), captorLoan.getValue().getEndDate()); assertEquals(loanDto.getStartDate(), captorLoan.getValue().getStartDate()); } @Test public void testAddLoan() { loanService.addLoan(loanDto1); Mockito.verify(loanDaoImplMock).addLoan(captorLoan.capture()); assertEqualsLoanCaptor(loanDto1, captorLoan); } /** * Test of deleteLoan method, of class LoanServiceImpl. */ @Test public void testDeleteLoan() { loanService.deleteLoan(loanDto1); Mockito.verify(loanDaoImplMock).deleteLoan(captorLoan.capture()); assertEqualsLoanCaptor(loanDto1, captorLoan); } /** * Test of updateLoan method, of class LoanServiceImpl. */ @Test public void testUpdateLoan() { loanService.updateLoan(loanDto2); Mockito.verify(loanDaoImplMock).updateLoan(captorLoan.capture()); assertEqualsLoanCaptor(loanDto2, captorLoan); } /** * Test of getLoanById method, of class LoanServiceImpl. */ @Test public void testGetLoanById() { Mockito.stub(loanDaoImplMock.getLoanById(Mockito.anyInt())).toReturn(loan1); loanDto1 = loanService.getLoanById(loan1.getId()); Mockito.verify(loanDaoImplMock).getLoanById(loan1.getId()); assertEqualsLoan(loan1, loanDto1); } @Test public void testGetLoansByEmployee() { List<Loan> list = new ArrayList<>(); list.add(loan1); employee1.setId(1); Mockito.stub(loanDaoImplMock.getListOfLoans()).toReturn(list); List<LoanDTO> result = loanService.getLoansByEmployee(employeeDto1); List<LoanDTO> expected = new ArrayList<>(); expected.add(loanDto1); assertEquals(expected, result); } /** * Test of getListOfLoanes method, of class LoanServiceImpl. */ @Test public void testGetListOfLoanes() { List<LoanDTO> loanDtoList = new ArrayList<>(); List<Loan> loanList = new ArrayList<>(); when(loanDaoImplMock.getListOfLoans()).thenReturn(new ArrayList<Loan>()); List<LoanDTO> returnedDtoList = loanService.getListOfLoanes(); assertTrue(returnedDtoList.isEmpty()); loanDtoList.add(loanDto1); loanDtoList.add(loanDto2); loanList.add(loan1); loanList.add(loan2); when(loanDaoImplMock.getListOfLoans()).thenReturn(loanList); List<LoanDTO> loanDtoListTemp = loanService.getListOfLoanes(); for (LoanDTO loanDTO : loanDtoList) { assertTrue(loanDtoListTemp.contains(loanDTO)); } } }