-
Notifications
You must be signed in to change notification settings - Fork 38.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@Transactional test does not execute all JPA lifecycle callback methods #28228
Comments
Hi @hughwphamill, Thanks for opening your first issue for the Spring Framework. 👍🏻
You can still test the behavior of your JPA lifecycle callback methods with This usually means that you need to flush the state of the unit of work to the underlying database. In the Avoid false positives when testing ORM code note in the Testing chapter of the reference manual, we point out that invoking For your particular test class, you can make the In summary, the behavior you have encountered is to be expected since all interactions with the unit of work occur within the same transaction. To ensure that your JPA lifecycle callback methods are invoked, you have two choices.
I will add a note to the reference manual to point out the need to |
@sbrannen Thanks very much for the detailed explanation and for updating the docs! |
@hughwphamill, you're welcome! And thanks for providing feedback. |
Spring Boot v2.6.4
@Transactional
tests are useful to roll back any test data created during tests; however, when a Spring integration test is annotated with@Transactional
the following JPA lifecycle callback methods are not executed:@PostPersist
@PostLoad
@PreUpdate
@PostUpdate
Example project with tests demonstrating the issue here:
https://github.com/hughwphamill/transaction-test-entity-issue
I understand this is the desired behavior from a JPA perspective, but it's still undesirable from a test perspective.
@Transactional
is incredibly useful in Spring integration tests to roll back test data, while exercising your whole application end to end. If your business logic uses JPA entity listeners then your application can't be properly tested in a@Transactional
test.The text was updated successfully, but these errors were encountered: