Integration testing transactions and optimistic locking with Spring and JUnit

I did some badness today.

I wanted an integration test to check to see if my @Transactional method actually did rollback on an optimistic lock failure. It’s a pretty business critical method so I wanted to be sure that the whole @Transactional and @Version annotation voodoo actually works.

The approach I took was to inject, what I called a “slowRepository” into the object with the @Transactional method. The slowRepository is just a mockito mock of the object which “answers” (using “thenAnswer”) to the “findBy…” with some custom code.

The idea is that at some point during the @Transactional method the slowRepository is called and the “answer” code is invoked. In the test, the answer code follows along the lines of:

1. Grab the data it requires from the (not slow) repository (the repository the slowRepository is mocking)
2. Switch back the slowRepository for the not slow repository
3. Invoke the @Transactional method again on a separate thread
4. Sleep for a bit
5. Wake up and return the data from step 1

The @Transactional call on the separate thread completes and commits before slowRepository wakes up. When it does, slowRepository returns stale data and the @Transactional method then fails accordingly.

The problem I encountered was using the setters for my @Autowired dependencies on the bean with the @Transactional method. I needed to use the setters so that I could switch in and out the slowRepository, but couldn’t because Spring AOP proxies are implementations of an interface, not subclasses (by default – yes I realise you can get Spring to proxy the target class using CGLIB). I found this blog post which explains how to get at the target object behind a Spring proxy. I converted it to Scala:

…which is pretty ninja.

12,992 thoughts on “Integration testing transactions and optimistic locking with Spring and JUnit

  1. وقت بخیر، خودم امروز اتفاقی تو اینترنت با این وبسایت رسیدم
    و بدون اغراق خیلی خوشم اومد. اطلاعاتش کاربردی بود
    و کمتر همچین منبعی ببینم.

    به نظرم برای افراد مختلف ارزش دیدن داره.

    برای کسایی که دنبال اطلاعات کامل هستن حتما برن ببینن.
    در مجموع تجربه خوبی بود و احتمالا باز هم
    سر می‌زنم

    در دید کلی

    برای کسایی که قصد شروع دارن

    بازی انفجار آنلاین

    می‌گردن

    این برند

    به نظر گزینه باشه

    کاربردی دربیاد

    همچنین

    برندهایی مثل

    еnfejaronline محبوب

    و

    sibbet قوی

    تونستن کاربرا جذب کنن

    در آخر کار

    قابل قبول بود

    و

    در آینده

    استفاده خواهم کرد

    .

    my web-sіte – معرفی یک پلتفرم امن برای تجربه بازی انفجار (jeejow.ir)

  2. I do not even know how I ended up here, but I thought this post
    was good. I don’t know who you are but definitely you’re going to a famous blogger if you are not already ;
    ) Cheers!

  3. Thank you for some other informative blog. The place else could I am getting that
    type of information written in such a perfect method?
    I’ve a challenge that I’m simply now operating on, and
    I have been on the glance out for such info.

  4. I got this web site from my pal who informed me on the topic of this website and now this time I am browsing this site and reading very informative content at this place.

  5. در کل

    برای کسایی که قصد شروع دارن

    بازی‌های جایزه‌دار

    فعالیت دارن

    این سرویس آنلاین

    مطمئناً می‌تونه

    کاربردی دربیاد

    همچنین

    برندهایی مثل

    enfeјаronline معتبر

    و

    پلتفرم sibbet

    تونستن اعتماد جلب کنن

    جمع‌بندی کلی

    خوشم اومد

    و

    بدون شک

    استفاده خواهم کرد

    Look aat my websіte :: سبک زندگی
    سالم (Myrtis)

  6. به طور کلی

    برای اون دسته که

    شرط آنلاین

    در این زمینه مشغولن

    این پلتفرم

    به نظرم می‌تونه

    قابل توجه باشه

    از طرف دیگه

    پلتفرم‌هایی مثل

    سایت enfejaronline

    و

    sibbet شناخته شده

    حضور پررنگی دارن

    در پایان کار

    خیلی خوب بود

    و

    به زودی

    دوباره نگاهش می‌کنم

    Check oout my blog – مدیریت کسب و کار

  7. Sugar, Sugar is very much a strategic puzzle game. Drawing lines on the screen can help improve motor skills and control. The game also gives players a chance to play around with physics and angles, creating different paths that speed up or slow down the stream depending on the angle they are drawn.  Just saw the movie. I was actually impressed by it. isnt sugar rush copy righted to that movie reck it ralph? Anyways the screenshot looks good. Sugar, Sugar is very much a strategic puzzle game. Drawing lines on the screen can help improve motor skills and control. The game also gives players a chance to play around with physics and angles, creating different paths that speed up or slow down the stream depending on the angle they are drawn.  Enjoy endless fun as you work through delightful levels and sugary surprises!
    https://jfy-tech.com.vn/teaspinscasinouk-jackpot-games-a-review-for-uk-players/
    Buran Casino has designed their site with you in mind, making sure that gaming feels great from start to finish. burankazino en-ca With a slick UI and a range of games, you’ll value the blend of excitement and simplicity. Enticing promotions, solid security, and 24 7 support have you wondering what else they have in store. Is this the perfect gaming experience? The exploration doesn’t end here—what’s around the next corner might surprise you. Yes, the site is safe and legitimate. The site is secured with SSL encryption to protect player information and it operates under a gaming license from the government of Curacao. If you encounter any issues or have any questions, you can contact Buran Casino’s customer service. They can be reached via email at support@burancasino, by phone at +35627780669, or through live chat which is available 24 7.

  8. These generous offers can help you maximize your winnings and minimize your losses, you can double the fun and double the wins. Players can enjoy classic games like Fruit Fiesta and Cash Splash, 888slots is a serious contender in the online gambling market due to its excellent customer support. The idea behind this strategy is to have multiple bets working for you at the same time, tapwin casino review and free chips bonus 2023. Tapwin casino review and free chips bonus in this article, Australia casinos closed their doors. If you’re searching for information regarding on-line casino rules and you can subscribed operators, you’ll constantly notice it on the regulator’s webpages. As an alternative, go to any of the condition-by-county internet casino pages on this web site. Fantastic Nugget could have been totally incorporated which have DraftKings’ Dynasty Benefits, giving professionals an alternative choice in order to work VIP points. They certainly were much more interesting and diverse, and many away from as to the reasons Golden Nugget turned into popular.
    https://cyfrowastrefa.net/review-fortuna-casino-uk-a-practical-look-for-players-in-the-united-kingdom/
    But an authentic recreation of the Pathfinder experience suffers tremendously without the guiding hand of a Dungeon Master. Simply put, Kingmaker doesn\u2019t feel properly balanced.Its dungeons are mostly poorly designed trapfests stuffed with monsters to kill and little else of interest to do, and several of its core systems conspire less to keep things interesting and more to keep progress slow. The combat encounters, dungeons, and additional mechanics like kingdom management are flawed, uneven, and wildly divergent in quality. The reviews and comments posted on this site reflect the opinions of individual posters and do not reflect the views of Cannonball Read. Dear ΒΑΣΙΛΗΣ ΣΚΟΔΡΑΣ,Thank you for taking your time to provide the review. We truly appreciate your feedback.Our goal is to provide the best possible experience to our Customers and we are sorry that your experience on our platform has not been positive.We kindly invite you to contact our support team via our second email: complaints@kingmaker and we’ll make sure to sort out your issue asap! Please provide our support team with your account details and so we could review the details of the case.Sincerely,Kingmaker Team.

  9. It is really a great and helpful piece of info. I am glad that you shared this
    useful info with us. Please keep us informed like this.
    Thanks for sharing.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>