From ee8c28d787b75c37921ff5d6e0b672d5edb5b150 Mon Sep 17 00:00:00 2001 From: Michael Minella Date: Wed, 7 Dec 2016 14:32:37 -0500 Subject: [PATCH] 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. --- ...tDataSourceItemReaderIntegrationTests.java | 45 +++++++++++++------ .../datasource/DataSourceInitializer.java | 9 ++-- .../item/database/data-source-context.xml | 10 ++--- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/AbstractDataSourceItemReaderIntegrationTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/AbstractDataSourceItemReaderIntegrationTests.java index 241b1dc3c..164069472 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/AbstractDataSourceItemReaderIntegrationTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/AbstractDataSourceItemReaderIntegrationTests.java @@ -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)reader).setTransacted(false); diff --git a/spring-batch-infrastructure/src/test/java/test/jdbc/datasource/DataSourceInitializer.java b/spring-batch-infrastructure/src/test/java/test/jdbc/datasource/DataSourceInitializer.java index 92a863bbb..61409abd8 100644 --- a/spring-batch-infrastructure/src/test/java/test/jdbc/datasource/DataSourceInitializer.java +++ b/spring-batch-infrastructure/src/test/java/test/jdbc/datasource/DataSourceInitializer.java @@ -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

diff --git a/spring-batch-infrastructure/src/test/resources/org/springframework/batch/item/database/data-source-context.xml b/spring-batch-infrastructure/src/test/resources/org/springframework/batch/item/database/data-source-context.xml index ed0891d50..60e5f7253 100644 --- a/spring-batch-infrastructure/src/test/resources/org/springframework/batch/item/database/data-source-context.xml +++ b/spring-batch-infrastructure/src/test/resources/org/springframework/batch/item/database/data-source-context.xml @@ -1,9 +1,9 @@ - +