1

I am using spring embedded jdbc database configuration in spring context file for Junit testing. I am using in memory database for testing. I am trying to unit test only DAO layer and for unit testing I am using spring container's in memory database.

When I am running Junit test case I am not seeing first test case values in second test case (testAddressCreate in testAddressUpdate test case). I am not using @Before or @After in my Junit for now. I am not sure how spring is creating in memory database and starting it. According to behavior it seems that it is creating or resetting before each test case. Does anyone know about it? Also how can we connect to spring created in memory database. Please suggest.

Spring Configuration is :

<jdbc:embedded-database id="dataSource" type="HSQL">
    <jdbc:script location="classpath:createdb.sql" />

Junit test case is :

@ContextConfiguration(locations="classpath:test-context.xml")
@Transactional
public class GenericDaoImplTest extends AbstractTransactionalJUnit4SpringContextTests {

@Autowired
private GenericDao<Address, Long> addressDao;

@Test
public void testAddressCreate() {
    Address address = new Address();
    address.setAddress1("first address");
    address.setCity("First City");
    address.setCountry("First one");
    address.setPostalCode("22222");
    boolean result = addressDao.create(address);
    Assert.assertEquals(true, result);

    List<Address> listOfAddress = addressDao.findAll();
    Assert.assertNotNull(listOfAddress);
    for(Address addressTemp : listOfAddress){
        System.out.println(addressTemp.getAddress1());
        System.out.println(addressTemp.getAddressId());
    }
}

@Test
public void testAddressUpdate(){
    Address address = new Address();
    address.setAddress1("second address");
    address.setCity("Second City");
    address.setCountry("Second one");
    address.setPostalCode("11111");
    boolean result = addressDao.create(address);
    Assert.assertEquals(true, result);

    address.setAddress1("Updated Second Address");
    Assert.assertNotNull(addressDao.update(address));

    List<Address> listOfAddress = addressDao.findAll();
    Assert.assertNotNull(listOfAddress);
    for(Address addressTemp : listOfAddress){
        System.out.println(addressTemp.getAddress1());
        System.out.println(addressTemp.getAddressId());
    }
}

}

Andrei Stefan
  • 51,654
  • 6
  • 98
  • 89
Icoder
  • 31
  • 5
  • There is something wrong with your code or configuration... Nobody can tell you where without you providing the code. – Raedwald Jun 21 '14 at 21:41
  • @Raedwald I have added code and jdbc configuration. – Icoder Jun 21 '14 at 22:45
  • That's not really a test, unless your test is simply a test of whether or not Address instances can be created, have setters applied to them, and the create and update and findAll() methods don't explode. You've asserted nothing about the behavior of your methods. – MetroidFan2002 Jun 21 '14 at 23:08
  • @MetroidFan2002, question is not how I have written unit test, I am asking about spring jdbc embedded database lifecycle and how can we connect to spring created in memory database. – Icoder Jun 21 '14 at 23:12
  • 1
    The question is unclear. The way I'm reading it is that you're not seeing updates in your database that were made in one test method show up in another (even though you can't even be certain, in an automated fashion, that data updates were even made as you're *not asserting anything*). I'm not certain why you'd expect to see values saved in one test show up in another since you're extending a transactional test which should wrap each test method in a transaction so that any updates are rolled back prior to the next test's execution. – MetroidFan2002 Jun 21 '14 at 23:19
  • 1
    Okay so you mean to say because I am extending my Junit with AbstractTransactionalJUnit4SpringContextTests that is why it is rolling back any updates prior to each test case. Also I have added some asserts :). I am just doing this exercise for myself :) – Icoder Jun 21 '14 at 23:38

1 Answers1

0

By default, a @Transactional test method will rollback any changes made to the database during the test method. If you want to change this behavior you need to annotate your method with @Rollback(false). I don't think the documentation is specific about the default behavior, but the Javadocs mentions this here:

Retrieves the TransactionConfigurationAttributes for the specified class which may optionally declare or inherit @TransactionConfiguration. If TransactionConfiguration is not present for the supplied class, the default values for attributes defined in TransactionConfiguration will be used instead.

And the default value defined in TransactionConfiguration for rollback is "true". These being said, you need something like this to be able to keep the values from the first @Test method in the second one:

@Test
@Rollback(false)
public void testAddressCreate() {
    Address address = new Address();
    ...
}

For connecting at the in-memory HSQLDB, take a look at this post on Stackoverflow.

Community
  • 1
  • 1
Andrei Stefan
  • 51,654
  • 6
  • 98
  • 89
  • Thanks Andrei, that's what I was looking for :) +1 for your answer. – Icoder Jun 23 '14 at 06:04
  • @StackUser, if you believe my answer helped you and fixed your issue, please consider accepting it, so others will know this is a helpful and reliable information. – Andrei Stefan Jun 23 '14 at 06:08
  • I don't know how to accept it. I tried to increase the count of useful answer, it did not let me increase because I am new to stackoverflow and not much reputation here :(. – Icoder Jun 24 '14 at 20:44
  • See these instructions: http://meta.stackoverflow.com/questions/251078/how-to-update-and-accept-answers – Andrei Stefan Jun 24 '14 at 22:18