-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Adapt framework for testing, make tests use single transaction and be revertible #4414
-
It will be cool if there is an ability to revert changes produced from Spring batch job in test.
I tried to test some job or single step and found that typical Spring approach with idempotent tests using @Transactional
doesn't work here. Even if transaction attributes were provided to support existing transaction and validateTransactionState
is disabled.
So i would like to use it somehow like this
Expected Behavior
@Test @Rollback @Transactional void Should_create_batches_for_existing_payments() throws Exception { testInvoiceGenerator.generateInvoicesForBatch(invoicesCount); var expectedCreatedBatchCount = invoicesCount / batchSize; var jobExecution = jobLauncherTestUtils.launchJob(); int countPaymentBatchCount = countPaymentBatchCount(); assertThat(jobExecution.getStatus()).isEqualTo(COMPLETED); assertThat(countPaymentBatchCount).isEqualTo(expectedCreatedBatchCount); }
Current Behavior
There is no way to revert commits made by Spring batch transaction manager, or override transaction behaviour in tests.
Context
Spring batch test
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 3 comments 2 replies
-
Hi @fmbenhassine ,what do you think about it?
Do you have an ideas how to achieve such testing?
Beta Was this translation helpful? Give feedback.
All reactions
-
A job might perform several transactions (through its steps). What, specifically, are you looking for? Are you asking to revert all transactions performed by a job that is run in a single JUnit test?
Beta Was this translation helpful? Give feedback.
All reactions
-
Yes, provide a simple way to make all the transactions nested and revert them after test finished.
Beta Was this translation helpful? Give feedback.
All reactions
-
That should be the case if you use the same transaction manager defined in the test context and in Spring Batch's JobRepository
and steps.
Beta Was this translation helpful? Give feedback.
All reactions
-
I tried to test some job or single step and found that typical Spring approach with idempotent tests using @transactional doesn't work here
Even with a single step? To help you efficiently, please provide a minimal example that we can use to fully understand your expectations, see https://github.com/spring-projects/spring-batch/blob/main/ISSUE_REPORTING.md.
Even if transaction attributes were provided to support existing transaction and validateTransactionState is disabled.
Which transaction attributes did you provide? Isolation levels and some propagation levels are not supported in Spring's Test Framework: https://docs.spring.io/spring-framework/reference/testing/testcontext-framework/tx.html#testcontext-tx-enabling-transactions.
Again, for this kind of questions and to provide efficient support, please take some time to provide a minimal example.
Beta Was this translation helpful? Give feedback.