When running Fitnesse SliM test a transactional method on spring application, a SQLException thrown - Data Source is Closed.
Here is what happends: In my application, method importFile(String filename) reads a csv file and update data to a few tables in database. This is a transactional method of spring application. So that if updating one row of the file throws an exception, all updates of this row rollbacks and all other rows (including previous rows and next rows) keep on updating.
See related link: How to define rollback transactions which are called inside of a loop with spring aop?
The testing step on Query table SliM FitNesse page
1 SetUp page sets up the testing data with a setup file
|Query: Test Case | test_file_setup.csv |
| name | phone | email |
| tom | 123 | tom@test.com |
| dick | 456 | dick@test.com |
| harry | 789 |harry@test.com |
result: testing passed
2 Test page tests a testing file with a invalid row, so that when it executes this row it only rollbacks this row, but all other rows proceeds, however... the test failed and rollback all rows and closed the data source.
3 So when the tearDown which tries to test with a valid file and clean up the database failed too... thus the data source was closed
This is one SliM test but there is a query table on each SetUp, Test and tearDown page. My guessing is one Fitness test only init one Spring context instance. Tell me if I am wrong. However, this method is declared as a transaction on spring config. It rollbacks as expected, but the test doesn't.
Here is my testing class
public class TestCase{
private ImportFile importFile;
private String filename;
public TestCase(String filename){
this.filename = filename;
init();
}
public void init(){
//initialize the spring context
//and get beans...
importFile = (ImportFile) context.getBeans("importFile");
start();
}
public void start(){
//read rows from file...
for( ... loop per row...){
//this is a transactional method...
//it reads the file and update every row to the database
//it is supposed to row back one row (per loop)
//and continue if an exception thrown
//but it rollback the all rows and close the datasource
importFile.importFile(filename);
}
}
public List<Object> query(){
//read from database and return the test result to the Slim query table
...
cleanup();
}
public void cleanup(){
//close the spring context
...
}
}
It seems that on the test page, Fitnesse starts a new testing process (creates a new TestClass instance) on SetUp, Test, TearDown. However, the result proves my wrong?!