Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow database assertion to be defined as a bean #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@ private void verifyExpected(DbUnitTestContext testContext, DatabaseConnections c
if (logger.isDebugEnabled()) {
logger.debug("Veriftying @DatabaseTest expectation using " + annotation.value());
}
DatabaseAssertion assertion = annotation.assertionMode().getDatabaseAssertion();

DatabaseAssertion assertion;
if (StringUtils.hasLength(annotation.assertionBean())) {
assertion = testContext.getDatabaseAssertion(annotation.assertionBean());
} else {
assertion = annotation.assertionMode().getDatabaseAssertion();
}
List<IColumnFilter> columnFilters = getColumnFilters(annotation);
if (StringUtils.hasLength(query)) {
Assert.hasLength(table, "The table name must be specified when using a SQL query");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.lang.reflect.Method;

import com.github.springtestdbunit.assertion.DatabaseAssertion;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;

Expand Down Expand Up @@ -49,6 +50,13 @@ public interface DbUnitTestContext {
*/
DatabaseOperationLookup getDatbaseOperationLookup();

/**
* Returns the {@link DatabaseAssertion} implemented by the bean with the given name.
* @param databaseAssertionBeanName name of the database assertion bean
* @return database assertion
*/
DatabaseAssertion getDatabaseAssertion(String databaseAssertionBeanName);

/**
* Returns the class that is under test.
* @return The class under test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import javax.sql.DataSource;

import com.github.springtestdbunit.assertion.DatabaseAssertion;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dbunit.database.IDatabaseConnection;
Expand Down Expand Up @@ -236,6 +237,10 @@ public DatabaseOperationLookup getDatbaseOperationLookup() {
return (DatabaseOperationLookup) getAttribute(DATABASE_OPERATION_LOOKUP_ATTRIBUTE);
}

public DatabaseAssertion getDatabaseAssertion(String databaseAssertionBeanName) {
return testContext.getApplicationContext().getBean(databaseAssertionBeanName, DatabaseAssertion.class);
}

public Class<?> getTestClass() {
return (Class<?>) ReflectionUtils.invokeMethod(GET_TEST_CLASS, this.testContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import com.github.springtestdbunit.assertion.DatabaseAssertion;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.filter.IColumnFilter;

Expand Down Expand Up @@ -66,6 +67,14 @@
*/
DatabaseAssertionMode assertionMode() default DatabaseAssertionMode.DEFAULT;

/**
* The name of the database assertion bean to use for comparing datasets.
* This bean must implement the {@link DatabaseAssertion} interface.
* If defined, this supersedes the {@link ExpectedDatabase#assertionMode()} element.
* @return Database assertion bean to use
*/
String assertionBean() default "";

/**
* Optional table name that can be used to limit the comparison to a specific table.
* @return the table name
Expand Down