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.

5,764 thoughts on “Integration testing transactions and optimistic locking with Spring and JUnit

  1. (136) Start typing your search to have a list of suggestions. Start typing your search to have a list of suggestions. (136) Britain’s most loved smart panel heaters. (136) Britain’s most loved smart panel heaters. (18) Start typing your search to have a list of suggestions. Britain’s most loved smart panel heaters. (136) (136) (18) (136) Britain’s most loved smart panel heaters. Start typing your search to have a list of suggestions. Britain’s most loved smart panel heaters. (18) (136) (18) (136) Britain’s most loved smart panel heaters. Britain’s most loved smart panel heaters. Britain’s most loved smart panel heaters. Britain’s most loved smart panel heaters. (136) Britain’s most loved smart panel heaters. (18) 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.
    https://ecvme.univet.hu/discover-the-best-roll-xo-match-betting-strategies-for-australian-players/
    YouCut – AI Video Editor Let’s take a look at the CapCut video editor. Whenever you’re choosing a new app or tool, features are key. In short, you want to know how an app can help you work better. Want to join the ranks of video creators? One easy way to begin is with the free CapCut video editor. In an age of bite-sized, short-form videos, CapCut is a leading tool for making clips of your own. Sadly, Non-linear Video Editors tend not to be particularly simple and have a bit of a learning curve attached. AI Video Editor – Text to Video Explicit songs and videos are easily accessible through CapCut’s “Templates” and there’s no way to control this. Videos using profanity and inappropriate media are prevalent and there aren’t any content settings. Whether logged into CapCut or not, there wasn’t any difference in content.

  2. No doubt you’re eager to get started, mate. FYI, there are a few quick rules to go over before you move into a gravity-free realm. The King’s Crown, the Golden Chalice, the Sands of Space-Time, and the Mystical Ring are powerful symbols you can conjure on the reels. There are Free Spins, Multipliers, Majestic Scatters, and Tumble features to enjoy. You are no longer bound by earthly restraints in Gates of Olympus™ online slots. There is a vastness with this online casino as it makes it more suitable for the player to be available through the use of mobile phones, Unibet has grown to features online and live casino gaming. Here are a few tips on how you can use the Game of Thrones slot game online to win real money, experiences with gates of olympus in online casinos you can always reach out to the support team for any form of clarification. If you love slots, you can enjoy all your favorite online casino games for real money. Spend some time working out whether you like it enough to switch to the real version, game review gates of olympus anywhere.
    http://www.bharataero.com/heaps-o-wins-casino-australia-a-rewarding-online-slot-experience-for-local-players/
    Gates of Olympus is a video slot by Pragmatic Play that features high volatility and a 6-reel, 5-row grid with the Pay-Anywhere mechanic, which means a win is triggered when eight matching symbols land anywhere on the grid (for greater detail, see the mechanics below). Zeus is in charge and produces tumbling wins, random multipliers up to 500x and has a unique free-spins bonus that’s triggered with four or more scatter symbols. Gates of Olympus has a 96.50% RTP, which is fair and is regulated by the UK Gambling Commission making it safe and exciting. 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.

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>