cz.muni.pa165.carparkapp.Service.BranchServiceImplTest.java Source code

Java tutorial

Introduction

Here is the source code for cz.muni.pa165.carparkapp.Service.BranchServiceImplTest.java

Source

/*
 * 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.
 */
package cz.muni.pa165.carparkapp.Service;

import cz.muni.pa165.carparkapp.DAO.BranchDAO;
import cz.muni.pa165.carparkapp.Entities.Branch;
import cz.muni.pa165.carparkapp.dto.BranchDTO;
import cz.muni.pa165.carparkapp.serviceImpl.BranchServiceImpl;
import java.util.ArrayList;
import java.util.List;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.test.context.ContextConfiguration;

/**
 *
 * @author Michal Petko
 */
@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration(locations = { "classpath:META-INF/contextTest.xml" })
public class BranchServiceImplTest {
    @Autowired
    @InjectMocks
    BranchServiceImpl branchService = new BranchServiceImpl();

    @Mock
    BranchDAO branchDaoImplMock;

    BranchDTO branchdto1;
    BranchDTO branchdto2;

    Branch branch1;

    @BeforeClass
    public static void setUpClass() {
    }

    @AfterClass
    public static void tearDownClass() {
    }

    @Before
    public void setUp() {
        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");

        branchdto2 = new BranchDTO();
        branchdto2.setId(2);
        branchdto2.setAddress("Praha");
        branchdto2.setCompanyNumber("987654");
        branchdto2.setName("Pobocka 89");

        Mockito.when(branchDaoImplMock.createBranch(Matchers.any(Branch.class))).thenAnswer(new Answer<Branch>() {
            @Override
            public Branch answer(InvocationOnMock invocation) throws Throwable {
                Object[] args = invocation.getArguments();
                return (Branch) args[0];
            }
        });

        Mockito.when(branchDaoImplMock.updateBranch(Matchers.any(Branch.class))).thenReturn(true);

        Mockito.when(branchDaoImplMock.findBranch(branch1.getId())).thenReturn(branch1);

        Mockito.when(branchDaoImplMock.deleteBranch(Matchers.any(Branch.class))).thenReturn(true);

        Mockito.when(branchDaoImplMock.createBranch(null))
                .thenThrow(new DataAccessResourceFailureException("Error occured during storing Branch."));
        Mockito.when(branchDaoImplMock.updateBranch(null))
                .thenThrow(new DataAccessResourceFailureException("Error occured during updating Branch."));
        Mockito.when(branchDaoImplMock.deleteBranch(null))
                .thenThrow(new DataAccessResourceFailureException("Error occured during deleting Branch."));
        Mockito.when(branchDaoImplMock.findBranch(-1))
                .thenThrow(new DataAccessResourceFailureException("Error occured during getting Branch."));
    }

    @After
    public void tearDown() {
    }

    /**
     * Test of addBranch method, of class BranchServiceImpl.
     */
    @Test
    public void testAddBranch() {
        try {
            branchService.addBranch(null);
            fail();
        } catch (Exception e) {
        }
        BranchDTO result = branchService.addBranch(branchdto1);
        assertEquals(branchdto1, result);
    }

    /**
     * Test of deleteBranch method, of class BranchServiceImpl.
     */
    @Test
    public void testDeleteBranch() {
        try {
            branchService.deleteBranch(null);
            fail();
        } catch (Exception e) {
        }

        branchService.addBranch(branchdto1);
        boolean result = branchService.deleteBranch(branchdto1);
        assertEquals(true, result);
    }

    /**
     * Test of updateBranch method, of class BranchServiceImpl.
     */
    @Test
    public void testUpdateBranch() {
        try {
            branchService.updateBranch(null);
            fail();
        } catch (Exception e) {
        }
        branchdto1.setAddress("Praha");
        branchService.updateBranch(branchdto1);
        assertEquals(branchdto1.getAddress(), "Praha");
    }

    /**
     * Test of findBranch method, of class BranchServiceImpl.
     */
    @Test
    public void testFindBranch() {
        try {
            branchService.findBranch(-1);
            fail();
        } catch (Exception e) {
        }

        BranchDTO result = branchService.findBranch(1);
        assertEquals(branchdto1.getCompanyNumber(), result.getCompanyNumber());
    }

    /**
     * Test of findAllBranches method, of class BranchServiceImpl.
     */
    @Test
    public void testFindAllBranches() {
        List<Branch> list = new ArrayList<>();
        list.add(branch1);

        Mockito.when(branchDaoImplMock.getAllBranches()).thenReturn(list);

        List<BranchDTO> result = branchService.findAllBranches();

        assertEquals(1, result.size());
    }
}