2

I wrote several JUnit test classes, in a maven project that uses JDBC to connect to a h2 database. I have 8 test classes.

  • When I run All tests with IntelliJ, I have a JDBC error (on the last test class : WorkspaceTest.java):

org.h2.jdbc.JdbcSQLException: Table "WORKSPACE" not found; SQL statement:

(if you want the full trace, I'll put it, but it's kinda long).

note : All the 7 other test classes are passed.

  • When I run each test separately with IntelliJ, they all pass, 1 by 1.

  • When I run the tests with mvn clean install, i get a build failure, with the error (on the last test class : OWSContextTest.java):

update(org.orbisgis.server.mapcatalog.OWSContextTest): Table "FOLDER" not found; SQL statement:

note : All the 7 other test classes are passed.

  • When I run this test alone with mvn (mvn -Dtest=OWSContextTest test), the test passes.

The tests classes are supposed to be independent, as in each of them I have a @BeforeClass which drops the database, then creates it, and finally populates it, AND a @AfterClass which drops the database.

If you need anything else, I'll edit for it. I really have no clue whatsoever about the reason to this behavior.

Thanks for your help.

EDIT : I just ran All tests about 10 times on IntelliJ, and it worked about 6 times, and failed with the same error 4 times. Don't know if I can really trust intelliJ test mechanism tho.

EDIT2 : If it can help, I tried more things : I ran 7 out of my 8 tests classes, for all 8 ones (I ran 1 2 3 4 5 6 7 together, then 2 3 4 5 6 7 8 together, then ...) And they all passed (all combination of 7 out of 8)

Saffron
  • 682
  • 6
  • 14
  • If you get "table not found" errors, most likely the database is empty (contains no tables and no data). What is your database URL? If it's an in-memory database, you would need to ensure the database is initialised (the tables are created). – Thomas Mueller Jul 15 '13 at 12:31
  • the database URL is jdbc:h2:./target/testdb. I understand that the problem comes from tables not created, but I dont understand why they are not created. As I said, if I run tests 1 by 1, they all pass. I just tried to run all but one (all 8 posibilities), and they all passed, every time. – Saffron Jul 15 '13 at 12:46
  • @Thomas Mueller is it possible that, due to several successive connections to my database, H2 is blocking the last one? – Saffron Jul 15 '13 at 13:50
  • Could you try using an absolute URL, for example `jdbc:h2:/data/dbs/testdb`? Several successive connections shouldn't be a problem at all. – Thomas Mueller Jul 15 '13 at 17:07
  • Changed the URL, in intellij it seems to be building approximatively 4/5 times, and in maven it's never building. I start to believe it comes from my computer. The results are way too random for it to be code-related no ? I mean, each tests are running alone, they run when I run 7 of them together, but all 8 are not ? – Saffron Jul 16 '13 at 07:19
  • Maybe the problem is concurrency? Do the tests run in [parallel](http://stackoverflow.com/questions/423627/running-junit-tests-in-parallel)? If yes, does it work if you run them one after the other? – Thomas Mueller Jul 16 '13 at 12:50
  • I have done nothing for them to run in parallel, so if it's not by default, it's not parallel. I asked a friend to run all the tests on his machine with `mvn clean install`, and he had a build success... Can I safely assume it's a problem with my computer ? – Saffron Jul 16 '13 at 14:46

1 Answers1

0

Does it work if you use @BeforeClass to drop the database and recreate it? Maybe a test's @AfterClass is executed after another test's @BeforeClass for it takes more time than you've expected.

Yugang Zhou
  • 7,123
  • 6
  • 32
  • 60
  • I don't understand what you want to say. I already do a @BeforeClass to drop and recreate. – Saffron Jul 15 '13 at 13:23
  • @Saffron But you said " AND a AfterClass which drops the database." I'm saying does it work if you remove AfterClass? – Yugang Zhou Jul 15 '13 at 13:33
  • Sorry about that. I tried without all the @AfterClass, but I get the same error, so it's not a synchronization problem. – Saffron Jul 15 '13 at 13:43