TouK OpenSource Projects

pl.touk.ormtest
Class JdbcSpringTxTestRule

java.lang.Object
  extended by pl.touk.ormtest.SpringTxTestRule
      extended by pl.touk.ormtest.JdbcSpringTxTestRule
All Implemented Interfaces:
org.junit.rules.TestRule

public class JdbcSpringTxTestRule
extends SpringTxTestRule

Class for JUnit testing of Spring-based Jdbc DAOs. This class uses H2 in-memory database.
Tests using this class are very fast because they don't load spring application context although they can be used to test spring DAOs!
This class is very simple to use. An example is presented below. Although the example doesn't use spring DAOs it would be very similar if it did. The spring Jdbc DAOs, which extend Spring's JdbcDaoSupport, need a JdbcTemplate which is in fact referenced in the example below (bold fragment in method before).



 public class TransactionalTest {

   @Rule
   public JdbcSpringTxTestRule txContext = new JdbcSpringTxTestRule();

   @Before
   public void before() {
     // Prepare environment for every test in this class (transaction (new for every test) has already been open):
     SimpleJdbcTestUtils.executeSqlScript(
         new SimpleJdbcTemplate(txContext.getJdbcTemplate()),
         new ClassPathResource("test.sql"),
         true);
     txContext.getJdbcTemplate().execute(
         "INSERT INTO EXAMPLEENTITIES (name) VALUES ('" + firstExampleEntity.getName() + "')");
     firstExampleEntity.setId(txContext.getJdbcTemplate().queryForInt("SELECT LAST_INSERT_ID()"));
   }

   @After
   public void after() {
     // Clean-up after every test in this class. Transaction for the
     // last executed test has not yet been closed if it is needed:
     txContext.getJdbcTemplate().execute(
         "INSERT INTO EXAMPLEENTITIES (id, name) VALUES (1, 'some other name')"));
   }

   @Test
   public void shoudPersistEntityA() throws Exception {
     txContext.getJdbcTemplate().execute(
         "INSERT INTO EXAMPLEENTITIES (id, name) VALUES (2, 'some other name')"));
   }

   @Test
   public void shoudPersistEntityB() throws Exception {
     txContext.getJdbcTemplate().execute(
         "INSERT INTO EXAMPLEENTITIES (id, name) VALUES (2, 'some other name')"));
   }
 }
 
In above example, if the two tests are executed in parallel then each of them will be executed on different, completely independent in-memory H2 databases. Of course an ExampleEntity plain old java bean (POJO) with id and name properties would be needed for the above example to work.

Author:
Michał Sokołowski

Field Summary
 
Fields inherited from class pl.touk.ormtest.SpringTxTestRule
threadsPerTestClass, txManagers, txStatuses
 
Constructor Summary
JdbcSpringTxTestRule()
           
 
Method Summary
protected  void ensureTemplateInitialized()
           
 org.springframework.jdbc.core.JdbcTemplate getJdbcTemplate()
           
static void resetThreadsForCurrentTestClass()
           
 
Methods inherited from class pl.touk.ormtest.SpringTxTestRule
apply, commitTransactionAndBeginNewOne, dataSource, findInvokingTestClass, getThreads, rollBackTransactionAndBeginNewOne
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JdbcSpringTxTestRule

public JdbcSpringTxTestRule()
Method Detail

getJdbcTemplate

public org.springframework.jdbc.core.JdbcTemplate getJdbcTemplate()

ensureTemplateInitialized

protected void ensureTemplateInitialized()
Specified by:
ensureTemplateInitialized in class SpringTxTestRule

resetThreadsForCurrentTestClass

public static void resetThreadsForCurrentTestClass()

TouK sp. z o.o. s.k.a.

Copyright © 2010–2015 TouK s.k.a.. All rights reserved.