Defining the CompanyDao test case

Each CompanyDao method should have at least one test method defined. We will include exactly one test method per implemented CompanyDao method. In enterprise applications, we would expect many more scenarios to be covered than the ones identified in the code snippet that follows.

We have also included minimum logging, just enough to split the output when running the test cases. You may wish to add more logging to help analyze the results. The test code assumes that the ttt_company table has appropriate data. In Chapter 2, The Task Time Tracker Database, we added three rows so that we know there is data available. Additional checks would need to be done if we do not have a database with consistent testing data. The file listing is:

package com.gieman.tttracker.dao;

import com.gieman.tttracker.domain.Company;
import java.util.List;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

public class CompanyDaoTest extends AbstractDaoForTesting {

    public CompanyDaoTest(){}

    @Test
    public void testFind() throws Exception {

        logger.debug("
STARTED testFind()
");
        List<Company> allItems = companyDao.findAll();

        assertTrue(allItems.size() > 0);

        // get the first item in the list
        Company c1 = allItems.get(0);

        int id = c1.getId();

        Company c2 = companyDao.find(id);

        assertTrue(c1.equals(c2));
        logger.debug("
FINISHED testFind()
");
    }

    @Test
    public void testFindAll() throws Exception {

        logger.debug("
STARTED testFindAll()
");
        int rowCount = countRowsInTable("ttt_company");

        if(rowCount > 0){

            List<Company> allItems = companyDao.findAll();
            assertTrue("Company.findAll list not equal to row count of table ttt_company", rowCount == allItems.size());

        } else {
            throw new IllegalStateException("INVALID TESTING SCENARIO: Company table is empty");
        }
        logger.debug("
FINISHED testFindAll()
");
    }

    @Test
    public void testPersist() throws Exception {

        logger.debug("
STARTED testPersist()
");
        Company c = new Company();
        final String NEW_NAME = "Persist Test Company name";
        c.setCompanyName(NEW_NAME);

        companyDao.persist(c);

        assertTrue(c.getId() != null);
        assertTrue(c.getCompanyName().equals(NEW_NAME));

        logger.debug("
FINISHED testPersist()
");
    }

    @Test
    public void testMerge() throws Exception {

        logger.debug("
STARTED testMerge()
");
        final String NEW_NAME = "Merge Test Company New Name";

        Company c = companyDao.findAll().get(0);
        c.setCompanyName(NEW_NAME);

        c = companyDao.merge(c);

        assertTrue(c.getCompanyName().equals(NEW_NAME));

        logger.debug("
FINISHED testMerge()
");

    }

    @Test
    public void testRemove() throws Exception {

        logger.debug("
STARTED testRemove()
");
        Company c = companyDao.findAll().get(0);

        companyDao.remove(c);

        List<Company> allItems = companyDao.findAll();
      
        assertTrue("Deleted company may not be in findAll List", !allItems.contains(c) );

        logger.debug("
FINISHED testRemove()
");
    }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.144.248