Skip to content
Advertisement

Spring repository : Junit test succeeds on custom delete even when @Transactional is missing

I have a Spring repository that has a method like this :

JavaScript

When testing this method in a Junit Test, it does work as intended.

JavaScript

However, the delete does not work on our production DB. For it to work on the production DB, we had to add the @Transactional annotation to the method in the repository

JavaScript

This is an issue as the test works but it does not work in production. Is it possible to make this test fail when there is a transactional annotation missing? The tests are configured to run on an in-memory H2 database.

Advertisement

Answer

The reason it works in tests is that the @DataJpaTest annotation contains the @Transactional meta-annotation. So the whole test method is covered with a transaction, and this allows AlertRepository methods to run without errors.

If you want to make it fail in tests, you can override the @Transactional annotation, as follows:

JavaScript

See the documentation for more information on this behavior.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement