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.

13,071 thoughts on “Integration testing transactions and optimistic locking with Spring and JUnit

  1. After checking out a number of the blog articles on your blog, I honestly appreciate your technique of blogging.
    I book-marked it to my bookmark webpage list and will be checking back soon. Please visit
    my web site too and let me know your opinion.

  2. Howdy this is somewhat of off topic but I
    was wanting to know if blogs use WYSIWYG editors or if you
    have to manually code with HTML. I’m starting a
    blog soon but have no coding know-how so I wanted to
    get advice from someone with experience. Any help would
    be greatly appreciated!

  3. Astronaut transforms the traditional crash game formula by featuring a space explorer whose jetpack multiplier grows until an inevitable crash. Players must cash out before the astronaut’s equipment fails, with dual-bet panels allowing simultaneous strategies across two independent wagers. Be aware! You will not find the bookmaker app in the App Store. You can download it only from the 1win official website. The theoretical Return to Player for Astronaut sits at 97%, which is competitive for crash games. However, our hands-on testing revealed some interesting nuances. Over 150 rounds with ₹100 bets, we recorded an actual RTP of 95.8%, which falls within expected variance. Be aware! You will not find the bookmaker app in the App Store. You can download it only from the 1win official website.
    https://erbeyiplik.com.tr/sugar-rush-1000-review-sweet-wins-await-australian-players/
    Aloha! Cluster Pays video slot features 6 reels, five rows and zero pay lines. Yep, you heard that correctly; Aloha! Cluster Pays Slot doesn’t feature any pay lines! This completely new and unique game structure provides plenty of room for the game’s bonus features to really take hold and bring in those big wins. Keep on reading to find out all about the game’s features, now let’s get set up and started. We hope you enjoyed this Slot Tracker-enabled Aloha Cluster Pays slot review of Aloha Cluster Pays slot game. Our tool is ready for you to enjoy; it’s absolutely free. Head over to slottracker to download. One feature that makes this game really worth your while is the Free Spins. Land 3 Hawaiian Sunset Free Spins symbols on the reels and you’re in for a treat. Unlike other slots that are more concerned about themes, its Cluster Plays gameplay is its main claim to fame. Net Entertainment’s Aloha! video slot features a coin range of 10 pence to £200 per bet and a maximum jackpot of £10,000.

  4. Awesome blog you have here but I was curious about if you knew of any user discussion forums
    that cover the same topics talked about here? I’d really like to
    be a part of online community where I can get feedback from
    other knowledgeable individuals that share the same
    interest. If you have any recommendations, please let me know.
    Bless you!

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>