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.

9,493 thoughts on “Integration testing transactions and optimistic locking with Spring and JUnit

  1. сколько стоит заказать курсовую работу сколько стоит заказать курсовую работу .

  2. new zealandn online pokies au reviews, online casino pokies australia and how to bet on united statesn roulette,
    or is online gambling in canada legal

    Feel free to surf to my homepage – winning edge craps (Maximo)

  3. south united states casinos, difference between european roulette
    and united kingdom and win real money online casino usa, or free
    spins Royal Caribbean Casino Tiers (Kompster.Com) no deposit bonus
    united states

  4. Gokkastenonline is een informatieve en vergelijkingssite voor online casino’s. Links, banners en andere afbeeldingen die leiden naar casino’s vanaf onze website, zijn advertenties en essentiële inhoud. Als u het er niet mee eens bent, kunt u zich afmelden You’re so cool! I don’t think I’ve read a single thing like this before. So wonderful to find someone with a few original thoughts on this issue. Really.. thanks for starting this up. This web site is something that is needed on the internet, someone with a bit of originality. We hadden geen achtergrondmuziek om naar te luisteren terwijl we speelden en de geluiden waren uw standaard klikken en dings, poker boeken. U heeft tot uw beschikking een filter dat u kan helpen een spel gemakkelijker te vinden, tutorial software. Het controleren van programma’s die garanderen dat games niet zijn opgetuigd wordt ook gedaan, gokken op voetbal wedstrijden uw spelvoorkeur selecteren.
    https://upperroomfireprayer.ca/club-2000-speelautomaten-de-nieuwste-releases-en-klassiekers/
    Veel, voornamelijk oudere fruitkasten, hebben een gokfunctie (gamble functie). Vaak is dit kop of munt. Hit je een prijs, dan kun je je prijs verdubbelen of verliezen door kop of munt te kiezen. Net zo lang totdat je verliest of wanneer de top bereikt is. Het maakt niet uit of je kop of munt kiest: allebei geven ze 50% kans op winst of verlies. Dit wordt ook wel neutraal gokken genoemd. Er komt hier overigens niks van behendigheid aan te pas. Runner Runner Megaways Een professionele coach wiens passie het is, mensen fysiek en mentaal te helpen ontwikkelen. Dit door middel van optimalisatie van voeding, training & mindset en betrokkenheid. Sommige spellen kunnen uitgesloten zijn of slechts gedeeltelijk meetellen voor het voldoen aan de inzetvereisten, en deze doorgegeven in algemeen gebruik.

  5. Наткнулся на развернутое руководство по продвижению в столице Сибири. Рассмотрены ключевые моменты: локальная оптимизация, подбор семантики, контент-маркетинг и внешние ссылки. Особо полезен раздел про адаптацию под мобильные и скорость загрузки. Ссылка: https://avto-dom-tumen.ru/stati/seo-v-novosibirske-polnoe-rukovodstvo-po-jeffektivnomu-prodvizheniju-sajta-v-stolice-sibiri/ . Рекомендую изучить.

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>