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.

10,912 thoughts on “Integration testing transactions and optimistic locking with Spring and JUnit

  1. Hello everyone, it’s my first pay a quick visit at this site, and piece of writing is really fruitful in support
    of me, keep up posting such posts.

  2. I loved as much as you’ll receive carried out right here.
    The sketch is tasteful, your authored material stylish.

    nonetheless, you command get got an shakiness over that you
    wish be delivering the following. unwell unquestionably come more
    formerly again as exactly the same nearly very often inside case you shield
    this increase.

  3. I savor, result in I found exactly what I used to be taking a
    look for. You’ve ended my 4 day lengthy hunt! God Bless
    you man. Have a great day. Bye

  4. доставка цветов по москве недорого курьером доставка цветов по москве недорого курьером .

  5. Just desire to say your article is as amazing. The clearness in your submit is just nice and i could suppose youre an expert in this subject. Well with your permission allow me to snatch your RSS feed to stay updated with approaching post. Thanks a million and please keep up the rewarding work. https://teletype.in/avtobloggerua/UJyFYOkOA7I

  6. My partner and I stumbled over here different web address and thought I might as well
    check things out. I like what I see so now i’m following you.
    Look forward to finding out about your web page yet again.

  7. Hey! I know this is kinda off topic nevertheless I’d figured I’d ask.

    Would you be interested in exchanging links or maybe guest writing a blog article or vice-versa?

    My blog discusses a lot of the same subjects as yours and I believe we could greatly
    benefit from each other. If you happen to be interested feel free to
    shoot me an email. I look forward to hearing from you!

    Superb blog by the way!

  8. Ребята, отличная статья для тех, кто хочет системно подойти к продвижению сайта в Екатеринбурге. Автор разбирает и SEO, и контекст, и соцсети, и email-маркетинг. Есть блок про актуальные тренды: мобильная адаптация, голосовой поиск, видеоконтент и ИИ. Очень структурированно, рекомендую: https://bioice-krasnoyarsk.ru/stati/prodvizhenie-sajtov-v-ekaterinburge-put-k-uspehu-v-cifrovom-prostranstve/

  9. Hey I know this is off topic but I was wondering if you knew
    of any widgets I could add to my blog that automatically
    tweet my newest twitter updates. I’ve been looking for a plug-in like this
    for quite some time and was hoping maybe you would have
    some experience with something like this. Please let me know if you run into anything.
    I truly enjoy reading your blog and I look forward to your new updates.

  10. Wow, superb blog layout! How long have you been blogging for?
    you made blogging look easy. The overall look
    of your website is excellent, as well as the content!

  11. Its not my first time to go to see this website, i am
    browsing this web page dailly and take nice information from here all the time.

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>