-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Execute all test methods in a class within the same transaction [SPR-5520] #10191
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
Comments
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. |
Sam Brannen commented Reopening this issue in light of #21093 and JUnit Jupiter's upcoming support for scenario tests. |
This comment was marked as off-topic.
This comment was marked as off-topic.
Any progress? @Suppress("UnnecessaryAbstractClass", "EmptyFunctionBlock")
@ContextBootTest
@TestInstance(value = TestInstance.Lifecycle.PER_CLASS)
@Execution(ExecutionMode.SAME_THREAD)
abstract class AbstractTransactionalScenarioTest {
@Autowired(required = true)
lateinit var transactionManager: PlatformTransactionManager
lateinit var status: TransactionStatus
@BeforeAll
private fun beforeTestClass() {
val td = DefaultTransactionDefinition().apply {
setName("ScenarioTestTx_${Thread.currentThread().id}")
propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRED
}
status = transactionManager.getTransaction(td)
beforeAll()
}
@AfterAll
private fun afterTestClass() {
afterAll()
transactionManager.rollback(status)
}
protected fun beforeAll() {}
protected fun afterAll() {}
} class ScenarioTest : AbstractTransactionalScenarioTest() {
override fun beforeAll() {
// class wide init code. ex, db init
}
@Test
@Transactional(propagation = Propagation.NESTED)
fun testA() {
// test 1...
}
@Test
@Transactional(propagation = Propagation.NESTED)
fun testB() {
// test 2...
}
} |
Alternatively, you can create a custom Something like: class MyTestExecutionListener extends AbstractTestExecutionListener {
@Override
public void beforeTestClass(TestContext testContext) throws Exception {
PlatformTransactionManager tm = TestContextTransactionUtils.retrieveTransactionManager(testContext, null);
TransactionDefinition txDef = new DefaultTransactionDefinition();
TransactionStatus txStatus = tm.getTransaction(txDef);
// put necessary context variables to threadlocal if parallel execution is needed
....
}
@Override
public void afterTestClass(TestContext testContext) throws Exception {
// retrieve necessary context values.
....
tm.rollback(txStatus);
}
} Existing In addition, you can use JUnit5 |
@ttddyy Thank you for your reply! |
@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. |
@ttddyy The reason what I added the NESTED propagation is that I want to add common DB initialization per class. |
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) |
Uh oh!
There was an error while loading. Please reload this page.
Eric Jain opened SPR-5520 and commented
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.Affects: 2.5.6
Issue Links:
@BeforeAll
method in test class with@TestInstance
(PER_CLASS) in JUnit Jupiter ("is duplicated by")@Transactional
on TestNG@BeforeClass
methods in the TestContext framework1 votes, 1 watchers
The text was updated successfully, but these errors were encountered: