Database integration testing / unit-testing with Spring, JPA, JUnit and Maven

The exact name for this tests is disputable. Whether it should be “database integration testing” or “unit testing”. But anyway, it is about this:
Many applications’ service layer relies heavily on database operations (through a JPA provider, for example), even though the access to the database is abstracted in a DAO layer.
An option for pure unit tests is to mock the DAO handler (using Mockito, EasyMock, etc), but in most cases this would be either a futile excercise, or it will be too complex to create well-behaving mocks.

So, using the following:
– spring 2.5.6
– hibernate entity manager
– junit 4.4
– maven2

we should achieve smooth database integration testing.
First, don’t try junit > 4.4, because spring 2.5.6 doesn’t work with it. Spring 3 will.

So, the steps.

  1. define your database access properties in .properites file and place it in src/main/resources (where the applicationContext.xml should reside as well). These properties should include: the dialect, the connection url, the username/password, the database driver.
  2. create src/test/resources, and create a properties file with the same name there, and set test-database parameters (using HSQLDB for example). Make the output folder for this source folder to be target/test-classes.
  3. in applicationContext.xml add
    <context:property-placeholder location="" />
  4. in src/test/java, in an appropriate package, create the following class:
    import org.junit.runner.RunWith;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    public abstract class BaseTest {
  5. Make all your test classes extend BaseTest (or alternatively, add those annotations on all classes)
  6. For additional capabilities, transactions, autowiring, etc, refer to the Spring documentation
  7. Run your unit tests either from within your IDE, or via maven. It works both ways.

2 Responses to “Database integration testing / unit-testing with Spring, JPA, JUnit and Maven”

  1. Spring Junit Test « Life in USA Says:

    […] A article forĀ Database integration testing / unit-testing with Spring, JPA, JUnit and Maven Like this:LikeBe the first to like this. […]

  2. 8 Ball pool cheats hack Says:

    Use the cover available to help you when you decide to make your move and pick your enemies off one at a time.
    This is a very no-frills version of pool with a relatively simple
    interface, and a large community still playing to
    this day, making it very easy to play a few quick games.
    Fuzzy’s Southwest Sports Grill, located at 18795 N.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: