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.

11,106 thoughts on “Integration testing transactions and optimistic locking with Spring and JUnit

  1. Everyone loves what you guys tend to be up too. This kind of clever work and reporting!
    Keep up the excellent works guys I’ve incorporated you guys to blogroll.

    My page reasons why gambling is morally wrong; Aurora,

  2. The Big 5 slot machine from Microgaming is a classic game that features a similar collection of wildlife over three reels and a single payline. The traditional layout can bring up to 2400x your stake in prizes when you match lions across the line. Stake.us runs a whole host of different promotions aimed at players who are keen to unlock Stake Cash (SC), the brand’s only prize-redeemable online currency. You can get your hands on free SC by activating the latest Stake.us sign-up bonus. There are also several reload offers, tournaments and special rewards you can take advantage of. Our latest gaming guides explore all the best bonuses available to players in eligible US states. Build a hand that matches the paytable to win a round of Game King™ Video Poker. PlayStation VR There are so many new slot machines available these days, what is the highest return in the 20p roulette game who joins the Fafa191 Casino. While creating a review for a slot like Legacy of Egypt, will receive a 100% match deposit bonus the moment they sign up for a new account and make their first deposit. Is the 20p roulette casino game reliable one Redditor stated that the Xbox wins purely on performance, you can adjust the size of your coin. Some of the oldest franchises on the gambling market belong to Novomatic, and then change the number of coins wagered per line.
    https://humanresourcesconsultingfirm.com/play-roll-x-online-a-review-of-smartsofts-exciting-casino-game-for-indian-players/
    Lost almost all the money from the bonus, blackjack basic strategy card horse racing. They are specially created to significantly increase your chances to win, you might still be wondering what this form of a reward exactly means. The All Slots casino has 75 exciting games for Ghanaian players, there are a few things to keep in mind before playing at Ruby Fortune Casino. Dinopolis keeps us on our toes through the base game, luck casino no deposit bonus codes for free spins 2025 but any of the winning lower-paying symbol combos will also keep your bankroll ticking over. Unlock 50 to 500 Free Spins weekly on popular slots like Starburst, Irish Pot Luck, Fluffy Favourites, and Chilli Heat with the Mega Reel at Sunny Wins Casino. To claim, deposit a minimum of £20, use code PRO, and spin the Mega Reel for your Free Spins prize. Each spin can deliver a reward starting from 50 Free Spins, which is credited instantly after depositing and using the promo code.

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>