It seems Spring's @DirtiesContext
is a:
Test annotation which indicates that the ApplicationContext
associated with a test is dirty and should therefore be closed and removed from the context cache.
Use this annotation if a test has modified the context — for example, by modifying the state of a singleton bean, modifying the state of an embedded database, etc. Subsequent tests that request the same context will be supplied a new context.
This is quite different than clearing the database by rolling back the current transaction: this destroys and recreates the entire Spring context.
The same effect can be achieved using Quarkus @QuarkusTestProfile
, described here as:
If a test has a different profile to the previously run test then Quarkus will be shut down and started with the new profile before running the tests. This is obviously a bit slower, as it adds a shutdown/startup cycle to the test time, but gives a great deal of flexibility.
If however all you need is to make sure that the transaction is rolled back after the test, all you need to do is to annotate the test method, not the controller with io.quarkus.test.TestTransaction
, as described here:
You can use the standard Quarkus @Transactional
annotation on tests, but this means that the changes your test makes to the database will be persistent. If you want any changes made to be rolled back at the end of the test you can use the io.quarkus.test.TestTransaction
annotation. This will run the test method in a transaction, but roll it back once the test method is complete to revert any database changes.
Your application code will run in the transaction started by the test method, do its thing that you can then test in the test method and finally rollback to wipe any persistent state that participates in the transaction. This has a few pitfalls: (1) If at some point your logic starts a new transaction, this is not rolled back automatically. (2) If your logic alters some kind of state that does not participate in the transaction, this change is not rolled back.