I am running JUnit 4.10 on Eclipse 3.7. I am doing database testing: database used is SQLite3 with sqlitejdbc. I tried running tests on both windows and linux and here are my observations:
- On Windows 7 x64, the tests run faster (~0.6s per test). I do get errors about “Cannot open database file”. They appear to be intermittent (sometimes the same test can pass once while the next run fails)
- On Ubuntu 11.10 x64, the tests are slow (~3s per test). I get errors about “database locked”. Unlike Windows’ errors being intermittent, here, errors seems to happen on the same test/file and once the lock error occur all following tests fails.
UPDATE
You can find the code for tests on GitHub. I think the problem might be something to do with how my @BeforeClass, @Before and test suites are setup
I have a Test Suite like
@RunWith(Suite.class)
@SuiteClasses({ DataAccessTests.class, SimpleQueryTests.class, ... })
public class DataAccessTestSuite {
@BeforeClass
public static void setUpDatabase() throws SQLException, ClassNotFoundException {
DataAccess.setEnvironment(DataAccess.DATABASE_TESTING);
Connection conn = DataAccess.getConn();
// truncate tables: simply DELETE statements
truncateTables(conn);
// insert test data, INSERT statements
insertTestData(conn);
}
Then in my Test classes I had a @Before that calls setup also.
public class EventsDataAccessTests {
@Before
public void setup() throws SQLException, ClassNotFoundException {
DataAccessTestSuite.setUpDatabase();
}
I was thinking I still want to setup my database in case I don’t want to call the entire suite. I think the problem is something to do with this, but I am not sure how exactly it causes a problem. It appears that even if I call the individual test class, my setup for the suite runs? Or if not, how can I insure it is run once in case the test is not called from the Suite?
— Old Post Removed —
You need to make sure no more than one thread at a time tries to have a write-lock on the sqlite database. For an overview of what I mean by that, read
(5) Can multiple applications or multiple instances of the same application access a single database file at the same time?
So when you say:
I think it’s fair to assume that the write-lock is not released by perhaps a connection object while another one tries to acquire it. So even if you don’t use threads, perhaps the GC doesn’t get to the connection object of a previous test before another test creates another connection object.
Maybe you can post some of these tests to help better understand what might be going wrong.
UPDATE:
Shouldn’t you close the connection here?: