Package org.flywaydb.test.dbunit
Class DBUnitTestExecutionListener
java.lang.Object
org.springframework.test.context.support.AbstractTestExecutionListener
org.flywaydb.test.dbunit.DBUnitTestExecutionListener
- All Implemented Interfaces:
org.springframework.core.Ordered,org.springframework.test.context.TestExecutionListener
public class DBUnitTestExecutionListener
extends org.springframework.test.context.support.AbstractTestExecutionListener
implements org.springframework.test.context.TestExecutionListener
A
Define a bean in the application context that implement the interface
TestExecutionListeners to get the annotation DBUnitSupport
up and running.
Attention: at the moment this implementation is only tested with H2 and
Oracle!
The annotation DBUnitSupport
DBUnitSupport.loadFilesForRun()use this with the DBUinit database operation and the load file (classpath ressource)-
DBUnitSupport.saveFileAfterRun()to store the result after run. ifDBUnitSupport.saveTableAfterRun()is not omitted a whole database table export will be done. Otherwise only the given tables will be exported.
FlywayTest are used the system
properties for jdbc.url, jdbc.driver,
jdbc.username, and jdbc.password should be set.
Also the test application context should contains code like this:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"><value>jdbc.properties</value></property>
<property name="ignoreResourceNotFound" value="true"/>
</bean>
<!-- H2 Setup
-Djdbc.driver=org.h2.Driver
-Djdbc.url=jdbc:h2:./db/testCaseDb
-Djdbc.username=OC_MORE_TEST
-Djdbc.password=OC_MORE_TEST
-->
<bean id="dataSourceId" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>$jdbc.driver</value></property>
<property name="url"><value>$jdbc.url</value></property>
<property name="username"><value>$jdbc.username</value></property>
<property name="password"><value>$jdbc.password</value></property>
</bean>
If this setup is used exist the possibility to run test again different
database such as H2 or Oracle.
Usage inside the TestClass
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/context/simple_applicationContext.xml" })
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
DBUnitTestExecutionListener.class })
public class SimpleLoadTest {
@Test
@DBUnitSupport(saveFileAfterRun = "./dbunit/result/oneMandatorCreate.xml", saveTableAfterRun = {
"MANDATOR", "" })
public void oneMandatorCreate() throws SQLException
{
...
}
@Test
@FlywayTest
@DBUnitSupport(loadFilesForRun={"INSERT","dbunit/insert/oneMandatorInsert.xml"})
public void oneMandatorInsert() throws SQLException
{
....
}
}
If the DefaultDatabaseConnectionFactory do not fit for usage with DBunit it can be customized.Define a bean in the application context that implement the interface
DatabaseConnectionFactory.
Afterwards the own implementation of the database connection factory will be used for generating database connection.
- Version:
- 1.7
- Author:
- Florian Eska
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected DatabaseConnectionFactoryprotected final org.apache.commons.logging.LogFields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE -
Constructor Summary
ConstructorsConstructorDescriptionAllocates newAbstractDbSpringContextTestsinstance. -
Method Summary
Modifier and TypeMethodDescriptionvoidafterTestClass(org.springframework.test.context.TestContext testContext) voidafterTestMethod(org.springframework.test.context.TestContext testContext) Only annotation with saveIt will be executedvoidbeforeTestClass(org.springframework.test.context.TestContext testContext) Only annotation with loadFiles will be executedvoidbeforeTestMethod(org.springframework.test.context.TestContext testContext) Only annotation with loadFiles will be executedprotected org.dbunit.database.IDatabaseConnectiongetConnection(DataSource dataSource, org.springframework.test.context.TestContext context) Get the dbunit specific database connectionintgetOrder()voidprepareTestInstance(org.springframework.test.context.TestContext testContext) voidsetOrder(int order) change the default order value;Methods inherited from class org.springframework.test.context.support.AbstractTestExecutionListener
afterTestExecution, beforeTestExecutionMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.test.context.TestExecutionListener
afterTestExecution, beforeTestExecution
-
Field Details
-
logger
protected final org.apache.commons.logging.Log logger -
dbConnectionFactory
-
-
Constructor Details
-
DBUnitTestExecutionListener
public DBUnitTestExecutionListener()Allocates newAbstractDbSpringContextTestsinstance.
-
-
Method Details
-
beforeTestClass
public void beforeTestClass(org.springframework.test.context.TestContext testContext) throws Exception Only annotation with loadFiles will be executed- Specified by:
beforeTestClassin interfaceorg.springframework.test.context.TestExecutionListener- Overrides:
beforeTestClassin classorg.springframework.test.context.support.AbstractTestExecutionListener- Throws:
Exception
-
prepareTestInstance
public void prepareTestInstance(org.springframework.test.context.TestContext testContext) throws Exception - Specified by:
prepareTestInstancein interfaceorg.springframework.test.context.TestExecutionListener- Overrides:
prepareTestInstancein classorg.springframework.test.context.support.AbstractTestExecutionListener- Throws:
Exception
-
beforeTestMethod
public void beforeTestMethod(org.springframework.test.context.TestContext testContext) throws Exception Only annotation with loadFiles will be executed- Specified by:
beforeTestMethodin interfaceorg.springframework.test.context.TestExecutionListener- Overrides:
beforeTestMethodin classorg.springframework.test.context.support.AbstractTestExecutionListener- Throws:
Exception
-
afterTestMethod
public void afterTestMethod(org.springframework.test.context.TestContext testContext) throws Exception Only annotation with saveIt will be executed- Specified by:
afterTestMethodin interfaceorg.springframework.test.context.TestExecutionListener- Overrides:
afterTestMethodin classorg.springframework.test.context.support.AbstractTestExecutionListener- Throws:
Exception
-
afterTestClass
public void afterTestClass(org.springframework.test.context.TestContext testContext) throws Exception - Specified by:
afterTestClassin interfaceorg.springframework.test.context.TestExecutionListener- Overrides:
afterTestClassin classorg.springframework.test.context.support.AbstractTestExecutionListener- Throws:
Exception
-
getConnection
protected org.dbunit.database.IDatabaseConnection getConnection(DataSource dataSource, org.springframework.test.context.TestContext context) throws Exception Get the dbunit specific database connection- Parameters:
dataSource-- Returns:
- Throws:
Exception
-
setOrder
public void setOrder(int order) change the default order value;- Since:
- 3.2.1.1
-
getOrder
public int getOrder()- Specified by:
getOrderin interfaceorg.springframework.core.Ordered- Overrides:
getOrderin classorg.springframework.test.context.support.AbstractTestExecutionListener- Returns:
- order default 4500
- Since:
- 3.2.1.1
-