Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Adapt framework for testing, make tests use single transaction and be revertible #4414

Unanswered
romchellis asked this question in Q&A
Discussion options

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

You must be logged in to vote

Replies: 3 comments 2 replies

Comment options

Hi @fmbenhassine ,what do you think about it?
Do you have an ideas how to achieve such testing?

You must be logged in to vote
0 replies
Comment options

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?

You must be logged in to vote
2 replies
Comment options

Yes, provide a simple way to make all the transactions nested and revert them after test finished.

Comment options

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.

Comment options

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.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
status: waiting-for-reporter Issues for which we are waiting for feedback from the reporter
Converted from issue

This discussion was converted from issue #4408 on July 10, 2023 08:57.

AltStyle によって変換されたページ (->オリジナル) /