Fix broken tests due to database initialization not occuring

This addes @DirtiesContext to some unit tests that were breaking because
database initialization was not occuring.
This commit is contained in:
Michael Minella
2016-12-07 14:32:37 -05:00
parent 8a2b23d669
commit ee8c28d787
3 changed files with 42 additions and 22 deletions

View File

@@ -15,24 +15,26 @@
*/
package org.springframework.batch.item.database;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import javax.sql.DataSource;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.sample.Foo;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
/**
* Common scenarios for testing {@link ItemReader} implementations which read
* data from database.
@@ -74,7 +76,9 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests {
/*
* Regular scenario - read all rows and eventually return null.
*/
@Transactional @Test
@Test
@Transactional
@DirtiesContext
public void testNormalProcessing() throws Exception {
getAsInitializingBean(reader).afterPropertiesSet();
getAsItemStream(reader).open(executionContext);
@@ -102,7 +106,9 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests {
* source and restore from restart data - the new input source should
* continue where the old one finished.
*/
@Transactional @Test
@Test
@Transactional
@DirtiesContext
public void testRestart() throws Exception {
getAsItemStream(reader).open(executionContext);
@@ -131,7 +137,9 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests {
* source and restore from restart data - the new input source should
* continue where the old one finished.
*/
@Transactional @Test
@Test
@Transactional
@DirtiesContext
public void testRestartOnSecondPage() throws Exception {
getAsItemStream(reader).open(executionContext);
@@ -163,7 +171,9 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests {
/*
* Reading from an input source and then trying to restore causes an error.
*/
@Transactional @Test
@Test
@Transactional
@DirtiesContext
public void testInvalidRestore() throws Exception {
getAsItemStream(reader).open(executionContext);
@@ -197,7 +207,9 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests {
/*
* Empty restart data should be handled gracefully.
*/
@Transactional @Test
@Test
@Transactional
@DirtiesContext
public void testRestoreFromEmptyData() throws Exception {
getAsItemStream(reader).open(executionContext);
@@ -209,7 +221,9 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests {
* Rollback scenario with restart - input source rollbacks to last
* commit point.
*/
@Transactional @Test
@Test
@Transactional
@DirtiesContext
public void testRollbackAndRestart() throws Exception {
getAsItemStream(reader).open(executionContext);
@@ -239,7 +253,9 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests {
* Rollback scenario with restart - input source rollbacks to last
* commit point.
*/
@Transactional @Test
@Test
@Transactional
@DirtiesContext
public void testRollbackOnFirstChunkAndRestart() throws Exception {
getAsItemStream(reader).open(executionContext);
@@ -262,8 +278,10 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests {
assertEquals(foo1, reader.read());
assertEquals(foo2, reader.read());
}
@Transactional @Test
@Test
@Transactional
@DirtiesContext
public void testMultipleRestarts() throws Exception {
getAsItemStream(reader).open(executionContext);
@@ -306,6 +324,7 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests {
//set transaction to false and make sure the tests work
@Test
@DirtiesContext
public void testTransacted() throws Exception {
if (reader instanceof JpaPagingItemReader) {
((JpaPagingItemReader<Foo>)reader).setTransacted(false);

View File

@@ -16,9 +16,14 @@
package test.jdbc.datasource;
import java.io.IOException;
import java.util.List;
import javax.sql.DataSource;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
@@ -34,10 +39,6 @@ import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import javax.sql.DataSource;
import java.io.IOException;
import java.util.List;
/**
* Wrapper for a {@link DataSource} that can run scripts on start up and shut
* down. Us as a bean definition <br><br>

View File

@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<tx:annotation-driven/>