diff --git a/spring-test-dbunit/src/main/java/com/github/springtestdbunit/DbUnitRunner.java b/spring-test-dbunit/src/main/java/com/github/springtestdbunit/DbUnitRunner.java index 6d7ebed1..f1f09ef2 100644 --- a/spring-test-dbunit/src/main/java/com/github/springtestdbunit/DbUnitRunner.java +++ b/spring-test-dbunit/src/main/java/com/github/springtestdbunit/DbUnitRunner.java @@ -132,12 +132,13 @@ private void verifyExpected(DbUnitTestContext testContext, DatabaseConnections c throws Exception, DataSetException, SQLException, DatabaseUnitException { String query = annotation.query(); String table = annotation.table(); - IDataSet expectedDataSet = loadDataset(testContext, annotation.value(), modifier); + List expectedDataSets = loadDatasets(testContext, annotation.value(), modifier); IDatabaseConnection connection = connections.get(annotation.connection()); - if (expectedDataSet != null) { + if (expectedDataSets.size() > 0) { if (logger.isDebugEnabled()) { logger.debug("Veriftying @DatabaseTest expectation using " + annotation.value()); } + IDataSet expectedDataSet = new CompositeDataSet(expectedDataSets.toArray(new IDataSet[expectedDataSets.size()])); DatabaseAssertion assertion = annotation.assertionMode().getDatabaseAssertion(); List columnFilters = getColumnFilters(annotation); if (StringUtils.hasLength(query)) { @@ -156,6 +157,15 @@ private void verifyExpected(DbUnitTestContext testContext, DatabaseConnections c } } + private List loadDatasets(DbUnitTestContext testContext, String[] dataSetLocations, DataSetModifier modifier) throws Exception { + List dataSets = new ArrayList(); + for (String dataSetLocation : dataSetLocations) { + IDataSet dataSet = loadDataset(testContext, dataSetLocation, modifier); + dataSets.add(dataSet); + } + return dataSets; + } + private DataSetModifier getModifier(DbUnitTestContext testContext, Annotations annotations) { DataSetModifiers modifiers = new DataSetModifiers(); for (ExpectedDatabase annotation : annotations) { diff --git a/spring-test-dbunit/src/main/java/com/github/springtestdbunit/annotation/ExpectedDatabase.java b/spring-test-dbunit/src/main/java/com/github/springtestdbunit/annotation/ExpectedDatabase.java index 14811fce..0f960576 100644 --- a/spring-test-dbunit/src/main/java/com/github/springtestdbunit/annotation/ExpectedDatabase.java +++ b/spring-test-dbunit/src/main/java/com/github/springtestdbunit/annotation/ExpectedDatabase.java @@ -54,11 +54,11 @@ String connection() default ""; /** - * Provides the location of the dataset that will be used to test the database. + * Provides the locations of the datasets that will be used to test the database. * @return The dataset locations * @see DbUnitConfiguration#dataSetLoader() */ - String value() default ""; + String[] value() default {}; /** * Database assertion mode to use. Default is {@link DatabaseAssertionMode#DEFAULT}. diff --git a/spring-test-dbunit/src/test/java/com/github/springtestdbunit/expected/ExpectedOnClassAndMethodComposedTest.java b/spring-test-dbunit/src/test/java/com/github/springtestdbunit/expected/ExpectedOnClassAndMethodComposedTest.java new file mode 100644 index 00000000..07a290a9 --- /dev/null +++ b/spring-test-dbunit/src/test/java/com/github/springtestdbunit/expected/ExpectedOnClassAndMethodComposedTest.java @@ -0,0 +1,54 @@ +/* + * Copyright 2002-2016 the original author or authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.springtestdbunit.expected; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestExecutionListeners; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; +import org.springframework.transaction.annotation.Transactional; + +import com.github.springtestdbunit.DbUnitTestExecutionListener; +import com.github.springtestdbunit.annotation.ExpectedDatabase; +import com.github.springtestdbunit.assertion.DatabaseAssertionMode; +import com.github.springtestdbunit.entity.EntityAssert; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("/META-INF/dbunit-context.xml") +@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, + DbUnitTestExecutionListener.class}) +@ExpectedDatabase(value = {"/META-INF/db/expectedfail_composed1.xml", + "/META-INF/db/expectedfail_composed2.xml", "/META-INF/db/expectedfail_composed3.xml"}) +@Transactional +public class ExpectedOnClassAndMethodComposedTest { + + @Autowired + private EntityAssert entityAssert; + + @Test + @ExpectedDatabase( + value = {"/META-INF/db/expected_nonstrict_composed1.xml", + "/META-INF/db/expected_nonstrict_composed2.xml"}, + assertionMode = DatabaseAssertionMode.NON_STRICT) + public void shouldUseMethodExpectation() { + this.entityAssert.assertValues("existing1", "existing2"); + } + +} diff --git a/spring-test-dbunit/src/test/resources/META-INF/db/expected_nonstrict_composed1.xml b/spring-test-dbunit/src/test/resources/META-INF/db/expected_nonstrict_composed1.xml new file mode 100644 index 00000000..fb047998 --- /dev/null +++ b/spring-test-dbunit/src/test/resources/META-INF/db/expected_nonstrict_composed1.xml @@ -0,0 +1,4 @@ + + + + diff --git a/spring-test-dbunit/src/test/resources/META-INF/db/expected_nonstrict_composed2.xml b/spring-test-dbunit/src/test/resources/META-INF/db/expected_nonstrict_composed2.xml new file mode 100644 index 00000000..1846520a --- /dev/null +++ b/spring-test-dbunit/src/test/resources/META-INF/db/expected_nonstrict_composed2.xml @@ -0,0 +1,4 @@ + + + + diff --git a/spring-test-dbunit/src/test/resources/META-INF/db/expectedfail_composed1.xml b/spring-test-dbunit/src/test/resources/META-INF/db/expectedfail_composed1.xml new file mode 100644 index 00000000..7165b327 --- /dev/null +++ b/spring-test-dbunit/src/test/resources/META-INF/db/expectedfail_composed1.xml @@ -0,0 +1,4 @@ + + + + diff --git a/spring-test-dbunit/src/test/resources/META-INF/db/expectedfail_composed2.xml b/spring-test-dbunit/src/test/resources/META-INF/db/expectedfail_composed2.xml new file mode 100644 index 00000000..b7613b85 --- /dev/null +++ b/spring-test-dbunit/src/test/resources/META-INF/db/expectedfail_composed2.xml @@ -0,0 +1,4 @@ + + + + diff --git a/spring-test-dbunit/src/test/resources/META-INF/db/expectedfail_composed3.xml b/spring-test-dbunit/src/test/resources/META-INF/db/expectedfail_composed3.xml new file mode 100644 index 00000000..acdb64da --- /dev/null +++ b/spring-test-dbunit/src/test/resources/META-INF/db/expectedfail_composed3.xml @@ -0,0 +1,4 @@ + + + +