question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Introduce reactive @Transactional support in the TestContext framework

See original GitHub issue

Since we have @Transactional working with R2DBC repositories in 1.0 M2 (as said here), I would like to ask if there is a way to make @Transactional working with JUnit (integration) tests (the same way we are able to do when using JDBC repositories). Is this currently possible? Will this even be possible? What is the right approach to achieve transactional tests ?

Currently, running a @Transactional @SpringBootTest gives me java.lang.IllegalStateException: Failed to retrieve PlatformTransactionManager for @Transactional test (the same problem as this guy has: http://disq.us/p/2425ot1).

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:6
  • Comments:18 (6 by maintainers)

github_iconTop GitHub Comments

7reactions
JoseLioncommented, Aug 11, 2020

Hey @mp911de! I had some trouble with this lately, and since the @Transactional annotation is not supported yet I came out with an small helper that transform any publisher to a rollback operation:

@Component
public class Transaction {

  private static TransactionalOperator rxtx;

  @Autowired
  public Transaction(final TransactionalOperator rxtx) {
    Transaction.rxtx = rxtx;
  }

  public static <T> Mono<T> withRollback(final Mono<T> publisher) {
    return rxtx.execute(tx -> {
      tx.setRollbackOnly();
      return publisher;
    })
    .next();
  }

  public static <T> Flux<T> withRollback(final Flux<T> publisher) {
    return rxtx.execute(tx -> {
      tx.setRollbackOnly();
      return publisher;
    });
  }
}

Then I can use it on tests like this:

@Test
void finds_the_account_and_return_it_as_user_details() {
  accountRepo.save(newAccount)
    .map(Account::getUsername)
    .flatMap(userDetailsService::findByUsername)
    .as(Transaction::withRollback) // <-- This makes the test rollback after the transaction
    .as(StepVerifier::create)
    .assertNext(user -> {
      assertThat(user.getUsername()).isEqualTo("mock@test.com");
    })
    .verifyComplete();
}

I thought this helper can be used by the @Transactional annotation in some way. Maybe the annotation can find all publishers within the @Test and add the transformer right before the StepVerifier (if present?). I’m not sure how possible/easy that might be though 😅. Another approach I was thinking is to add this right into the StepVerifier (again, not sure if possible since it requires Spring to work) or to another implementation of the StepVerifier (something like TxStepVerifier perhaps?). Of course, this approaches will not use the @Transactional annotation, but will add more control over which publisher should rollback and which should not 🙂

Hope this helps, at least as a brainstorm on some ways to solve the issue.

Cheers!!

3reactions
JoseLioncommented, Jun 3, 2021

I’m glad this helped @nhajratw 🙂

In case anyone is interested, I went one step further and created a transactional step verifier:

public interface TxStepVerifier extends StepVerifier {

  static <T> FirstStep<T> withRollback(final Mono<T> publisher) {
    return StepVerifier.create(publisher.as(Transactions::withRollback));
  }

  static <T> FirstStep<T> withRollback(final Flux<T> publisher) {
    return StepVerifier.create(publisher.as(Transactions::withRollback));
  }
}

This makes things a little bit easier to use 😁 so based on the same example above the usage would be:

@Test
void finds_the_account_and_return_it_as_user_details() {
  accountRepo.save(newAccount)
    .map(Account::getUsername)
    .flatMap(userDetailsService::findByUsername)
    .as(TxStepVerifier::withRollback) // <-- This makes the test rollback after the transaction
    .assertNext(user -> {
      assertThat(user.getUsername()).isEqualTo("mock@test.com");
    })
    .verifyComplete();
}

Cheers!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Programmatic Transactions in the TestContext Framework
Spring has excellent support for declarative transaction management throughout application code as well as in integration tests.
Read more >
Testing - Spring
See transaction management with the TestContext framework. 3.1.4. Support Classes for Integration Testing. The Spring TestContext Framework ...
Read more >
Index (spring-test 5.0.5.RELEASE API) - Javadoc.io
Abstract base test class which integrates the Spring TestContext Framework with explicit ApplicationContext testing support in a JUnit environment.
Read more >
Why Spring Framework is Popular - DataFlair
ii. New test execution callback methods introduced in Spring TestContext Framework using TestNG, JUnit 5 via SpringRunner. iii. XMLUnit support is upgraded to ......
Read more >
What's new in Spring Framework 5?
JDK baseline update · Core framework revision · Core container updates · Functional programming with Kotlin · Reactive Programming Model · Testing ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found