The situation and the problem
I have several test classes, each with several test methods. All tests use the same test database in the background. Each test class initializes its database contents and then tests stuff in several test methods.
When I run each test individually, they all pass. But when I run several tests at the same time (either using maven or my IDE, IntelliJ), the methods of different test classes are run interleaved, eg. the database initialization of the second class runs after the first class has started but before all test methods of first class have been run so these methods will fail (because the database already contains second class's data).
Some things I've tried, and some more details
The simplest solution would be to force the TestNG runner to run the classes in succession (ie. wait for all the test methods of a test class to finish before running test methods of another class). Can this be done?
I can probably do this by specifying each class as a separate test in my suite, but I don't want to do this as this means I'd have to add something to the suite whenever I add a test class which is clumsy and error-prone.
Simply asking TestNG to not parallelize anything (eg. setting thread count to 1 or disabling parallel running) doesn't help here since methods still get run in the wrong order (though not simultaneously).
One option would be to use a different database for each test class, but I don't see a simple way to do this (using JPA and Guice).
I'm not currently using DBUnit, Unitils etc.; I don't know these tools very well but I got the impression the don't solve my problems.
I'm using JPA to initialize database in each test class (ie. create entity objects and presist them).
Use method interceptors (=write and enable a custom one) to control the ordering.
Also, each class might use its own database sandbox: wrapping each test class into a transaction might possibly help.
Having individual suffix for tables/db set up for each class would even let your test methods run in parallel (I guess the first two options would not).