mohamed
mohamed

Reputation: 85

How to keep Maven integration tests isolated from each other?

I have multiple Maven integration tests that are updating the the state of the database, which could create conflicts between these tests. I was wondering if there is a way to isolate these integration tests by leveraging Maven phases or any other approach? Ideally, I would like to have a way to run database migrations before every integration test class. I am using Flyway as the migration tool for my PostgreSQL database and I am using JUnit 4.12. The migrations that I am running are basically creating and populating tables with data for testing.

Upvotes: 0

Views: 455

Answers (3)

mohamed
mohamed

Reputation: 85

I was able to solve this using flyway-core. Basically I ended up doing the following inside each of the test classes:

@BeforeClass
public static void migrateDB(){
    Flyway flyway = Flyway.configure().dataSource(url, user, password).load();
    flyway.clean();
    flyway.migrate();
}

Upvotes: 0

Dima Patserkovskyi
Dima Patserkovskyi

Reputation: 743

Responsibilities of Maven is to run tests one by one on integration-tests phase, and check results on verify. It also able to prepare/shutdown environment. Check failsafe plugin.

And all isolation between tests is a responsibility of a test framework you use (JUnit, TestNG, Cucumber, etc.).

Upvotes: 0

Junit has @Before and @After annotations to let it invoke methods before and after each test class.

Those methods are then responsible for bringing the database into a known state before each test.

Upvotes: 1

Related Questions