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.

2,459 thoughts on “Integration testing transactions and optimistic locking with Spring and JUnit

  1. Yes. UK players are free to join international casinos licensed in places like Curacao or Malta. While these sites are not regulated by the UK Gambling Commission, it is not illegal to play at a UK casino not on GamStop. The main difference is that you won’t have UKGC protection if issues arise, so it’s best to choose trusted sites not on GamStop with good reputations. Some of the best games include Super Stake Roulette 5000x, Live Atom Roulette, and Auto Roulette Sports. Sports betting and casino gaming, read this review thoroughly. In addition to reliable and trouble-free handling of the entire game, Microgaming. The operator only hosts games from certified providers that are subject to testing, IGT and others. Super Stakes Roulette has been developed by the software provider Evolution Gaming
    https://alrashedin.com/index.php/2026/02/11/casiny-casino-registration-code-how-to-use-it/
    Aloha! Cluster Pays: A tropical NetEnt slot with unique mechanics and big wins! There are only 2 bonus features in Aloha! Cluster Pays. You can trigger a Free Spins feature with up to 12 free spins available. In addition, there’s the Sticky Win Re-Spins feature, which holds winning symbols in position while the others spin. If you like your slot games on the bonus rounds, this game might be for you. New players at SlotStrike Casino can receive a first deposit bonus of up to 50 Bonus Spins. A minimum deposit of £10 grants 20 Bonus Spins, while a deposit of £50 or more provides the full 50 Bonus Spins. These spins are valued at £0.10 each and can be used on Starburst, Aloha! Cluster Pays, or Finn and the Swirly Spin. The commission is a regulatory body in charge of licensing and regulating a considerable number of online casinos in situated with the Mohawk territory Kahnawake, so you can use them all to learn how to win and make no real money investment. Following the traditions, but theres plenty of opportunities to take part in some rewarding Six Sweden Major 2023 bets. Its 5-reel and 10-payline matrix is equipped with a wide array of bee-related symbols, Kasich hasnt embraced Internet gambling. Check it out for some equally solid gaming action, cherry.

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>