You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Execute all test methods in a class within the same transaction [SPR-5520] #10191
Being able to configure that all test methods in a class should be run in the same transaction would be useful for people using TestNG (which supports test method dependencies) to write functional tests.
This feature could also be useful in JUnit Jupiter when using @TestInstance(PER_CLASS) or the planned support for scenario tests.
@ttddyy Thank you for your reply!
But sadly, I tried your annotation style solution but it didn't work properly.
It seems rollback didn't work for each test case.
(@Transactional(propagation = Propagation.NESTED) doesn't work?)
@junichimiyazaki I just wrote this sample impl and seems working to me. It starts and rollbacks transaction at class level. You can extend and add per method behavior if you want.
Just looking back your example, you want to add the NESTED propagation in each test method with rollback?? I'm not sure why you want to do it.
Can it be simply start/rollback transaction on each method, which is exactly what TransactionalTestExecutionListener do by default.
@ttddyy
I'm sorry, I was misunderstanding it. Your case seems to fully satisfy the requirements of this ticket.
The reason what I added the NESTED propagation is that I want to add common DB initialization per class.
Because it simplifies and speeds up each test cases.
Activity
spring-projects-issues commentedon Apr 15, 2015
Sam Brannen commented
Since this issue has received 0 votes in the last 6 years, I am resolving it as "Won't Fix".
However, if the community deems this issue still relevant, this issue may potentially be reassessed at a later date.
spring-projects-issues commentedon Mar 9, 2018
Sam Brannen commented
Reopening this issue in light of #21093 and JUnit Jupiter's upcoming support for scenario tests.
genzhewozou commentedon Oct 14, 2020
junichimiyazaki commentedon Dec 17, 2020
Any progress?
I found a workaround as below but I think this code is a bit hacky.
So, I hope this feature be supported officially.
ttddyy commentedon Dec 20, 2020
Alternatively, you can create a custom
TestExecutionListenerthat start/stop a transaction at the test class level.Something like:
Existing
TransactionalTestExecutionListeneris a good source for implementation details.In addition, you can use JUnit5
@TestMethodOrderto ensure the testing order within the test class.junichimiyazaki commentedon Dec 22, 2020
@ttddyy Thank you for your reply!
But sadly, I tried your annotation style solution but it didn't work properly.
It seems rollback didn't work for each test case.
(
@Transactional(propagation = Propagation.NESTED)doesn't work?)ttddyy commentedon Dec 22, 2020
@junichimiyazaki I just wrote this sample impl and seems working to me. It starts and rollbacks transaction at class level. You can extend and add per method behavior if you want.
Just looking back your example, you want to add the NESTED propagation in each test method with rollback?? I'm not sure why you want to do it.
Can it be simply start/rollback transaction on each method, which is exactly what
TransactionalTestExecutionListenerdo by default.junichimiyazaki commentedon Dec 28, 2020
@ttddyy
I'm sorry, I was misunderstanding it. Your case seems to fully satisfy the requirements of this ticket.
The reason what I added the NESTED propagation is that I want to add common DB initialization per class.
Because it simplifies and speeds up each test cases.
GeenEva commentedon Dec 31, 2020
I found this solution on StackOverflow and it saved the day:
Add the @DirtiesContext annotation, but provide it with the AFTER_EACH_TEST_METHOD classMode
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)