When testing your JPA entity handling code with a real entity manager it will not only write your entities to a real database, it will also call all entity listeners you have defined. Your entity listeners might be easy to fix but sometimes it’s just better to remove the entity listeners from the entity manager so they are ignored.
Entity listeners are methods in the entity class annotated with special annotation (such as @PerPersist
or @PostDelete
), or they can be defined in custom classes which are associated with the entity using the @EntityListeners
annotation on the entity itself.
Depending on your entity listeners it can be advantageous to disable all entity listeners during tests instead of trying to get them to run during tests; they might access singletons or frameworks that are not active during tests, and mocking or stubbing those would seriously distract from the test itself.
How entity listeners are created from annotations and