-
Notifications
You must be signed in to change notification settings - Fork 35
Usage flyway dbunit test
flyway-dbunit-test add a test annotation support DBUnitSupport that can used together with annotation FlywayTest.
The new annotation can be used on class and method level.
This annotation will be part of the project until DBunit will support a own annotation. The annotation only support a subset of the feature of DBUnit.
The annotation can only be used if you are using spring-test. For a easier startup flyway-test-extension deliver a simple aplication context ready to use.
Inside the maven setup example you will configure the database connection and a basic directory for flyway SQL scripts.
Attention
flyway-dbunit-test has currently Spring 4 dependency. If Spring 3 dependency needed use flyway-dbunit-spring3-test instead.
A complete project that use flyway-dbunit-test you will find flyway-test-sample-dbunit.
For your project under test we must extent you Maven pom.xml file.
First step we include the dependency to flyway-dbunit-test. The project will bring a set of dependencies so you additional only need dependency to junit and your database driver.
<dependency> <groupId>org.flywaydb.flyway-test-extensions</groupid> <artifactId>flyway-dbunit-test</artifactid> <version>4.0</version> <scope>test</scope> </dependency>
Second show a example Maven profile to run test again database H2. If you are use a different database use the database specific jdbc driver and jdbc URL.
<profile> <id>h2-test</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupid> <artifactId>maven-surefire-plugin</artifactid> <configuration> <systemPropertyVariables> <jdbc.driver>org.h2.Driver</jdbc.driver> <jdbc.url> jdbc:h2:nio:${project.build.directory}/db/flywaytest.db </jdbc.url> <jdbc.username>h2_test</jdbc.username> <jdbc.password>h2_test</jdbc.password> <flyway.locations>sampletest3</flyway.locations> </systempropertyvariables> <threadCount>1</threadcount> </configuration> <goals> <goal>test</goal> </goals> <executions> </executions> </plugin> </plugins> </build> </profile>
The used system properties will be used from the simple_applicationContext.xml file of `flyway-spring-test`.
Used system properties:
- jdbc.driver - name of the database jdbc driver
- jdbc.url - connection url for the database (without username and password) here we use a H2 file dabasse with NIO and the database file will be created `inside target/db` directory
- jdbc.username - login user name for the database
- jdbc.password - login password for the database
- flyway.locations - basic directories for the SQL installation and migration SQL statements.
mvn test -P h2-test
For the test implementation it is important to setup spring test execution. A implementation example you will find here Spring4DBUnitTest.java.
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"/context/simple_applicationContext.xml" }) @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, FlywayDBUnitTestExecutionListener.class })
- For this setup we use the spring runner fpr JUnit.
- The ContextConfiguration specifies which spring xml files should be loaded.
If you need more than one configuration file specify as seperate files in the location list.
The files normally will be searched in your classpath. - In the test execution listeners section we specify the flyway-dbunit-test extentsion FlywayDBUnitTestExecutionListener. These garantees that the both annotations [DBUnitSupport] and FlywayTest will invoke in the correct order.
If you need a database setup before you start any test you can use the annotation a class level.
@FlywayTest @DBUnitSupport(loadFilesForRun = { "INSERT", "/dbunit3/dbunit.cus1.xml" }) public class Spring3DBUnitTest
The database will be reseted afterwards the dbunit support will inserted all insert rows form the file `dbunit/dbunit.cus1.xml`. The test of the class will start after all test setups are done.
If you need a database setup before you specific test method, you can use the annotation at method level.
@Test @FlywayTest @DBUnitSupport( loadFilesForRun = { "INSERT", "/dbunit3/dbunit.cus1.xml" }) public void dummyTestMethodLoad()
Now the database will be reseted and filled with the specific dbunit insert rows before the test method will be executed.
If you need for additional test a stored version of test result after a test run you can use following specific usage of the annotation. This ispossbile on class and method level.
@DBUnitSupport( saveTableAfterRun = { "CUSTOMER", "select * from CUSTOMER" }, saveFileAfterRun = "target/dbunitresult/customer1.xml")
This will store the result of the CUSTOMER table that will be retieved with the specific SQL statement into the file `target/dbunitresult/customer1.xml`.
The default sample application context you will find it here simple_applicationContext.xml
If you need some otehr features you can set up a own application context and used it inside the test class set up.
The importent here is that you must configure a database connection and the flyway setup.
How to change the default database connection factory see Interface-DatabaseConnectionFactory