Transactional Gotchas in Spring Boot Tests

Ғылым және технология

Did you know that when you run transactional tests in Spring Boot applications, a rollback in the tests doesn’t cause a rollback in the database? I certainly didn’t. This video demonstrates the problem (and how at first I got “lucky” with JUnit default ordering of my tests), and how to fix it.
Testing transactions can be tricky, but it is manageable once you know how.
Tales from the jar side, newsletter: kenkousen.substack.com
Tales from the jar side, videos: / @talesfromthejarside
GitHub repository: github.com/kousen/shopping_v3
00:00 - Welcome
04:14 - Testing the Repository
08:00 - Testing the Controller in a Running Server
10:36 - A Naive Controller Test
12:39 - JUnit test order
15:33 - Transactions in tests != Transactions on server
23:40 - Conclusions

Пікірлер: 25

  • @shivaakrish
    @shivaakrish24 күн бұрын

    Today i was facing exactly the same issue. Thank you so much for this video👍🏻

  • @talesfromthejarside

    @talesfromthejarside

    24 күн бұрын

    I'm glad it helped, though I'm sorry you hit the same issue :⁠-⁠)

  • @Alexander-zc2ju
    @Alexander-zc2juАй бұрын

    Great video! Was facing some weird issues with my integration tests, but going to give this a try!

  • @talesfromthejarside

    @talesfromthejarside

    Ай бұрын

    Good luck! I hope that fixes the issues

  • @user-ut7md1tz7h
    @user-ut7md1tz7h Жыл бұрын

    Very solid explanation with plenty of minute details. It's nice to know about all those complicated intricacies. Saves a lot of time in practice. Thank you very much.

  • @talesfromthejarside

    @talesfromthejarside

    Жыл бұрын

    Thank you for the kind words! That's exactly why I made the video. I needed to get the whole issue clear in my head so I could explain it to others. I'm really glad it worked for you. :⁠-⁠)

  • @DineshkumarPuli
    @DineshkumarPuli Жыл бұрын

    Wow! i got the exact same issue, but could not figure out why it was failing. I ended up going with solution 1 without transactional!. Thank you very much from the valuable information! Beautifully detailed with enough information and background!

  • @talesfromthejarside

    @talesfromthejarside

    Жыл бұрын

    That's great to hear. I'm glad it wasn't just me :⁠-⁠)

  • @sphesihlemanuel2933
    @sphesihlemanuel29333 ай бұрын

    Thanks sir, someone actually use this theme

  • @diegomx80
    @diegomx80 Жыл бұрын

    Great explanation! Thanks for taking the time to explain the issue and the right way to fix it.

  • @talesfromthejarside

    @talesfromthejarside

    Жыл бұрын

    Thanks for the kind words. I'm really glad the video helped you :⁠-⁠)

  • @anton-tkachenko
    @anton-tkachenko4 ай бұрын

    Hey Ken! Whenever I see @Transactional annotation on tests, a few questions always arise: - why not just keep data in db / clean it when needed? [ I'm actually a "lucky" developer of a multitenant application, and for every test, I usually create a new scope - and run everything in parallel :) ] - are you sure any developer joining the project will understand that it's "legal" and that such tests can be trusted? What if the DBMS has deferred transactions? - what if tests have async flows? The example and the problem in the video are clear, but what is your preferred strategy across projects?

  • @talesfromthejarside

    @talesfromthejarside

    4 ай бұрын

    You're right that I'm dealing with the simplest cases. I have only a single database and only one tenant. That's what I often use in testing, however, and I find the automatic rollback on Spring tests to be very useful. In the more complex situations you describe, then naturally you have to be more careful about the data and when the transactions occur, though I would still expect them to be created and committed/rolled back at the beginning and end of each method. For async cases, I've mostly worked with reactive classes like StepVerifier. If I have async data, then yeah, I empty and refill the test tables before and after each test, as you suggested. I agree that I can't rely on transactions to reset in that situation. TBH, though, I think you probably have a lot more experience with those other situations than I do, so I welcome your opinion. :)

  • @d47im5e
    @d47im5e4 ай бұрын

    Very good information, thank you!

  • @talesfromthejarside

    @talesfromthejarside

    4 ай бұрын

    Glad it was helpful

  • @omnipoten8
    @omnipoten8Ай бұрын

    So spot on ! Thanks for sharing the valuable content. Really admire the work that you have put in making this video. Just one question (not related to the integration test) - The the Transactional annotation work when a given block of code is interacting with two DAOs in sequence. Example , after writing successfully via the first DAO , the write to second DAO failed - will this rollback the writes on first DAO ?

  • @talesfromthejarside

    @talesfromthejarside

    Ай бұрын

    Assuming you're using the default propagation attribute (Propagation.REQUIRED), then the second call joins the first one and they will roll back together. You can change that if you wish. Good luck!

  • @omnipoten8

    @omnipoten8

    Ай бұрын

    @@talesfromthejarside Thank you for the explanation.

  • @ErikWeibust
    @ErikWeibust Жыл бұрын

    Great video Ken! As always, you did a great job communicating something hard, and better yet, you had a solution. I guess you've changed your philosophy and have moved on from the test-server and client-server strategy and are now embracing the mock-server testing mode?

  • @talesfromthejarside

    @talesfromthejarside

    Жыл бұрын

    Hey Erik! Glad you liked the video. I'm not sure what I prefer when it comes to mocks vs real servers. I guess the key is to know the issues so you can use either wherever you think it's appropriate :⁠-⁠)

  • @dreb.7760
    @dreb.776013 күн бұрын

    Unfortunately, the solution with keeping the transactional annotation did not work for me. I am probably missing something. I think I'll have to go with solution where we clean up before each test. Thanks a bunch!

  • @LeonardoBPG
    @LeonardoBPG2 ай бұрын

    That is a hell of a trap!

Келесі