1

I am attempting to use H2 for my integration tests. I want the H2 database to reset to my data.sql between each test. But in suggested solutions, I have not found to be worked; @Transactional and @DirtiesContext does not seems to be working.

In a @beforeAll, I am setting up a mock queue (using the ElasticMQ library and wondering if this might also causing an issue?).

The Tests work fine when I run them on their own.

Any Thoughts would be greatly appreciated!

C Murphy
  • 313
  • 2
  • 11
  • I am not sure if you will like my suggestion; but as this is an integration test perhaps you shouldn't reset the database between each test, and write you assertions such that they take account of the fact the database is in a "used" state perhaps query for a specific row rather than select all and assume it should just have n number of rows? – Gavin Apr 17 '19 at 10:38
  • Well its more I am updating rows in the DB in my tests (eg. changing the Status of some entity) and then wanting to test change that row a different way in another test. Since the tests can run in any order it seems like a poor decision not have the DB reset each time. – C Murphy Apr 17 '19 at 10:41
  • If the other test isn't changing the status than perhaps you have an opportunity to be more specific in your testing; this test tests that the status updates the other test test some other aspect of the row? I think by having a clean database each time you often miss issues that only turn up when the database has data in it, of course such issues may never turn up :) – Gavin Apr 17 '19 at 10:45
  • 1
    Better to clean the database for each test and have things in a known state. – Nathan Hughes Apr 17 '19 at 17:46

1 Answers1

1

For some reason @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) solved my problem! I had not tried this earlier as @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) did not work for me.

C Murphy
  • 313
  • 2
  • 11