Skip to content
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 annotation doesn't work in JUnit tests #6463

Closed
sudhirtumati opened this issue Jan 8, 2020 · 4 comments · Fixed by #11798
Closed

@Transactional annotation doesn't work in JUnit tests #6463

sudhirtumati opened this issue Jan 8, 2020 · 4 comments · Fixed by #11798
Labels
area/testing kind/enhancement New feature or request
Milestone

Comments

@sudhirtumati
Copy link

Describe the bug
Database changes made during JUnit tests are not rolled back even when test class and/or method is marked @transactional

Expected behavior
Database changes must be rolled back when test method is annotated with @Transactional

Actual behavior
Database changes are not rolled back causing other tests to fail

To Reproduce
Steps to reproduce the behavior:

  1. Clone this sample repo
  2. Run mvn clean test from command line

Configuration

# datasource configuration
quarkus.datasource.url=jdbc:h2:~/test
quarkus.datasource.driver=org.h2.Driver

# drop and create the database at startup (use `update` to only update the schema)
quarkus.hibernate-orm.database.generation=drop-and-create

Screenshots
N/A

Environment (please complete the following information):

  • Output of uname -a or ver:
    Linux SUDHIR-TP 4.19.84-microsoft-standard #1 SMP Wed Nov 13 11:44:37 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
  • Output of java -version:
openjdk version "11.0.5" 2019-10-15
OpenJDK Runtime Environment 18.9 (build 11.0.5+10)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.5+10, mixed mode)
  • Quarkus version or git rev: 1.1.0.Final

Additional context
(Add any other context about the problem here.)

@sudhirtumati sudhirtumati added the kind/bug Something isn't working label Jan 8, 2020
@gsmet gsmet added kind/enhancement New feature or request area/testing and removed kind/bug Something isn't working labels Jan 8, 2020
@stuartwdouglas
Copy link
Member

This is not how @transactional works, it just starts a regular transaction that would be committed as normal. In addition this transaction is not propagated over JAX-RS calls, so even if it did roll back on the client side the server side transaction would commit as normal.

This would need a new annotation (we have talked about @TestTransaction in the past), and would require some work to automatically propagate the transaction over HTTP.

@sudhirtumati
Copy link
Author

With Spring (and of course SpringBoot), @Transactional works this way. Just annotating the test class with this annotation makes all tests run in separate transactions and all changes are rolled back after test is complete. I find this behavior very convenient.

PS: I am sure this is the behavior with MockMvc. I can try and see if it is the same case with RestAssured

@kostacasa
Copy link

kostacasa commented Sep 1, 2020

@sudhirtumati is right. Anyone coming from Spring Boot world would expect a "transactional test" to perform all persistence operations within a test-owned transaction that is rolled back at the end of that test.

The documentation mentions the concept of transactional tests both in the transaction guide and testing guide, but doesn't fully explain what the behavior is. This is undoubtedly making people jump to the wrong conclusion. I was convinced I was doing something wrong until I came across this issue.

P.S. Do you have a suggested methodology, even if manual, that would allow Spring-like behavior? I tried to call rollback on the test injected UserTransaction object manually at the end of the test, but that threw an exception with:
ARJUNA016108: Wrong transaction on thread

stuartwdouglas added a commit to stuartwdouglas/quarkus that referenced this issue Sep 2, 2020
This allows you to run tests in a rollback only transaction.

Fixes quarkusio#6463
stuartwdouglas added a commit to stuartwdouglas/quarkus that referenced this issue Sep 7, 2020
This allows you to run tests in a rollback only transaction.

Fixes quarkusio#6463
stuartwdouglas added a commit to stuartwdouglas/quarkus that referenced this issue Sep 7, 2020
This allows you to run tests in a rollback only transaction.

Fixes quarkusio#6463
mkouba pushed a commit to stuartwdouglas/quarkus that referenced this issue Sep 7, 2020
This allows you to run tests in a rollback only transaction.

Fixes quarkusio#6463
@gsmet gsmet added this to the 1.9.0 - master milestone Sep 8, 2020
@geoand
Copy link
Contributor

geoand commented Dec 15, 2020

With Spring (and of course SpringBoot), @Transactional works this way. Just annotating the test class with this annotation makes all tests run in separate transactions and all changes are rolled back after test is complete. I find this behavior very convenient.

PS: I am sure this is the behavior with MockMvc. I can try and see if it is the same case with RestAssured

This only works in Spring when you are running tests against the in-memory spring context - it does not work for RestAssured style tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/testing kind/enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants