Defining a test case superclass

The first step is to create a superclass that all of our DAO test cases will inherit. This abstract class looks like the following code snippet:

package com.gieman.tttracker.dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;

@ContextConfiguration("/testingContext.xml")
public abstract class AbstractDaoForTesting extends AbstractTransactionalJUnit4SpringContextTests {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired(required = true)
    protected CompanyDao companyDao;
    @Autowired(required = true)
    protected ProjectDao projectDao;
    @Autowired(required = true)
    protected TaskDao taskDao;
    @Autowired(required = true)
    protected UserDao userDao;
    @Autowired(required = true)
    protected TaskLogDao taskLogDao;
}

The AbstractDaoForTesting class is marked as abstract so that it cannot be instantiated directly. It provides member variables that are accessible to all the subclasses, thus removing the need to replicate code in the descendents. As a result, each subclass will have access to the DAO instances as well as the SLF4J logger. There are two new Spring annotations:

  • @ContextConfiguration: This annotation defines the Spring application context used to load the bean container. The testingContext.xml file has been covered in detail in the previous sections.
  • @Autowired: This annotation indicates to Spring that the container-managed bean with matching type should be dependency injected into the class. For example, the CompanyDao companyDao definition will result in Spring querying the container for an object with type CompanyDao. There is only one object with this type: the CompanyDaoImpl class that was discovered and configured by Spring when scanning the com.gieman.tttracker.dao package via the <context:component-scan base-package="com.gieman.tttracker.dao"/> entry in the testingContext.xml file.

The final important thing to notice is that the AbstractDaoForTesting class extends the Spring AbstractTransactionalJUnit4SpringContextTests class. Apart from being a very long class name, this class provides transparent transactional rollbacks at the end of each test method. This means the database state at the end of any DAO testing operations (including any insert, update, or delete) will be the same as at the start of testing. If this behavior is not required, you should extend AbstractJUnit4SpringContextTests instead. In this case any testing database operations can be examined and confirmed after the tests have been run. It is also possible to mark a single method with @Rollback(false) when using AbstractTransactionalJUnit4SpringContextTests to commit changes if required.

Let's now write our first test case for the CompanyDao operation.

..................Content has been hidden....................

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