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

  1. Getting the symbol in a row or column will result in a win, we check to see that their live baccarat games play perfectly on mobile. New release gates of olympus slot also on reel 3 during free spins there’s a symbol of the vampire and lady holding each other spinning on the reel in the background which is not visible, only to discover they didnt offer the crypto I wanted. The background of the entertainment gives the feeling of being on Mount Olympus, and the fiery image of the main character standing next to the machine adds drama and intrigue. The graphics and design are visually appealing to the user, making them feel a part of ancient Greek legends. The Gates of Olympus phenomenon rolls on, and it has never rolled as weightily as it does in Gates of Olympus Super Scatter. Potentially, that is. For those who are unable to land 4 super scatters on the board or who don’t manage to win more than 5,000x, which is probably most people, Gates of Olympus Super Scatter isn’t a lot different to Gates of Olympus the OG. This isn’t a bad thing per se. It’s not for nothing that Gates has become such a mega-popular release, after all. As far as tumbles, scatter wins, multipliers, and free spins go, Gates of Olympus set a sturdy benchmark for the genre.
    https://diraga.com/sunspin-casino-review-a-top-choice-for-australian-players/
    Untuk mengaktifkan iklan yang dipersonalisasi (seperti iklan berbasis minat), kami dapat membagikan data Anda dengan mitra pemasaran dan periklanan kami menggunakan cookie dan teknologi lainnya. Mitra tersebut mungkin memiliki informasi mereka sendiri yang telah mereka kumpulkan tentang Anda. Menonaktifkan pengaturan iklan yang dipersonalisasi tidak akan menghentikan Anda melihat iklan Demo Zeus Olympus 1000, tetapi dapat membuat iklan yang Anda lihat kurang relevan atau lebih berulang. Regardless of how you decide to play Gates of Olympus – for real money, after registering at the official casino site, or for free by launching the demo version – familiarising yourself with the video slot should always start with the payout table. This contains all the necessary information about the cost of symbols and how to unlock bonus features, as well as a brief instruction manual. It is marked with the ‘i’ icon.

  2. зарубежные казино онлайн Культура обслуживания в premium европейских казино напоминает об этикете старых гостиниц. Служба поддержки доступная 24/7 не просто решает технические проблемы но и способна дать рекомендацию по игре объяснить тонкости правил или помочь сориентироваться в акционных предложениях. Общение часто ведется не только на английском но и на языке игрока будь то немецкий французский испанский или один из скандинавских языков. Эта внимательность к деталям — от дизайна сайта выполненного в стиле минимализма или ар-деко до curated подборки музыки в разделах live-игр — формирует целостный образ казино как места для sophisticated аудитории ценящей качество надежность и глубину опыта выше мимолетных всплесков адреналина.

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>