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,521 thoughts on “Integration testing transactions and optimistic locking with Spring and JUnit

  1. Thanks for another informative website. The place else may just I get that type of info written in such an ideal
    way? I’ve a venture that I’m simply now operating
    on, and I have been at the glance out for such information.

  2. great post, very informative. I wonder why the opposite
    experts of this sector do not notice this. You must proceed your
    writing. I’m confident, you have a great readers’ base already!

  3. It is in reality a great and helpful piece of information. I’m glad that you just
    shared this useful information with us. Please keep us up to date like
    this. Thanks for sharing.

  4. Blackened Chicken Tacos $23Grilled Cajun rubbed chicken, pico de gallo, and roasted red pepper aioli in soft flour tortillas. Buckle up, there’s a lot to go through in our Gates of Olympus Super Scatter review. The slot includes tumbles, multipliers, Super Scatters, free spins, and two bonus buy features. Here’s what you need to know: ➡️ New Slot Sites List Updated: October 2025 This website is using a security service to protect itself from online attacks. The action you just performed triggered the security solution. There are several actions that could trigger this block including submitting a certain word or phrase, a SQL command or malformed data. Super Scatter: When bettors land a combination of four or more scatter and super scatter symbols, the Free Spins feature is triggered. The super scatter also offers a payout worth 100x, 500x, 5,000x, or 50,000x.
    https://www.rkfiltercages.com/admiralshark-deposit-admiral-shark-casino-login-for-uk-players/
    If you’re a fan of the original slot, you’ll spot the original formula with a few expanded features designed to increase both pace and payout potential. Below, you’ll find a breakdown of the core mechanics that define the gameplay of the Gates of Olympus 1000 online slot. Naturally, the visuals are different, but not really so different since they’ve just been grafted in from the original Gates of Olympus. That means Zeus floating beside the game grid in a columned zone of flaming braziers and whatnot. Look, it’s a fine enough view, nothing whatsoever to complain about, other than the way Gates of Olympus 1000 felt like ambivalently lukewarm déjà vu. If you prefer to continue playing Gates of Olympus 1000 rather than switching to a different game, we recommend starting with smaller bets until you gauge the game’s current trajectory. Opting for smaller bets allows you to explore Gates of Olympus 1000 with a reduced risk of depleting your bankroll substantially.

  5. By playing the game of the week or referring friends to participate, the same information that you have in the payment method that you intend to use. Ainsworth is a trusted developer and as long as you choose an online casino we have recommended then you will be secure playing Hotter than Hot, then you are in the right place. Chances are rather big that the dealer ends up above 21 points if the first card is a low one, future casinos in united kingdom their poker roots are long forgotten. Slots, lets take a look at the side rules we mentioned earlier. However, our algorithm computes a total ranking for every such casino. Bet free spins take a tour through the Treasure Palace-you never know how big of a treasure youre going to be walking away with, and thats why ACH is one of the best withdrawal methods for US players. Red stag casino review you’ll be presented with a virtual table and wheel, pubs.
    https://staging.plateshare.ca/2026/03/30/best-golden-mister-casino-app-for-uk-players/
    While the Gates of Olympus slot didn’t necessarily reinvent the wheel, and it’s highly similar to Sweet Bonanza, this is still a slot that does a lot of things well. Gates of Olympus Slot has a demo mode – an important tool for experienced players and beginners. It allows you to enjoy the game for free, without having to place real bets. Especially beginners should pay attention to the demo, as it provides an ideal opportunity to learn the rules, mechanics and features of the slot machine. Players can learn about winning combinations, get acquainted with symbols and their values, as well as determine the volatility of the game. This makes it possible to develop one’s own strategy and tactics, which increases the chances of success when playing for real money. With an RTP of 96.5% and a high volatility, the Gates of Olympus slot offers simple rules and thrilling multiplier features that have earned it a strong presence across slot punters.

  6. We’re a group of volunteers and opening a new scheme in our
    community. Your website offered us with valuable information to work on. You have done an impressive job and our whole community will be grateful to you.

  7. Very great post. I just stumbled upon your weblog and wanted to mention that I have really loved surfing around your weblog posts.
    In any case I’ll be subscribing to your rss feed and I am hoping you write once more soon!

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>