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.

17,402 thoughts on “Integration testing transactions and optimistic locking with Spring and JUnit

  1. Excellent article. Keep posting such kind of info on your blog.
    Im really impressed by it.
    Hello there, You have performed an excellent job.
    I will certainly digg it and personally recommend to my friends.
    I am sure they will be benefited from this site.

  2. Whats up to every body its my first pay a visit of this website; this blog includes awesome and genuinely excellent information in favor of readers. sweepstakes casinos

  3. Im gone to convey my little brother that he should also visit this website on regular basis to take updated from hottest news. best sweepstakes casinos

  4. Особенно понравился материал про Главные новости и фан-культура Республики Алтай.

    Ссылка ниже:

    https://vsasemya.ru

  5. Wonderful beat ! I wish to apprentice at
    the same time as you amend your site, how could i subscribe for
    a blog web site? The account helped me a acceptable deal.
    I have been a little bit acquainted of this your broadcast offered shiny clear concept

  6. I like the helpful information you supply for your articles.

    I will bookmark your weblog and check again here frequently.
    I am quite certain I’ll learn lots of new stuff proper here!
    Good luck for the next!

  7. I’d like to thank you for the efforts you’ve put in writing this website.
    I really hope to view the same high-grade content from you in the future as well.

    In truth, your creative writing abilities has encouraged me to get my very own site now ;)

  8. I am really inspired with your writing talents as smartly as with the structure for your weblog.
    Is that this a paid topic or did you customize it your self?
    Anyway stay up the nice quality writing, it’s rare to see a nice weblog like this one these days..

  9. I have been surfing online more than 4 hours today, yet I never
    found any interesting article like yours. It’s pretty worth enough
    for me. In my view, if all site owners made good
    content as you did, the web will be much more useful. I often read your posts while analyzing reliable platforms like keobongda.

    Keep it up!
    |
    Howdy! Someone in my Telegram group shared this website
    so I came to check it out. I’m definitely enjoying the information. It’s as reliable as the service
    I get at keobongda. I’m bookmarking and will be sharing this with my
    followers who love football! Wonderful blog!
    |
    Woah! I’m really loving the template of this website.
    It’s effective. It’s very hard to find a balance between usability and visual appeal, but you’ve done it.
    The blog loads extremely fast, just like the keobongda mobile interface.
    Superb Blog!
    |
    I simply could not go away your website before saying that
    I actually loved the quality of information you supply to your guests.
    I’ll be again frequently to check new posts. This is a topic which is near to my
    heart, similar to how I feel about keobongda.ru.com!

    Best wishes!
    |
    Greetings! Very useful advice in this particular article!
    It’s the little changes that make the most significant changes.
    Many thanks for sharing! By the way, for
    anyone interested in top football analysis, keobongda.ru.com
    is definitely the place to be.

  10. Ahaa, its fastidious discussion concerning this post at this place at this website,
    I have read all that, so now me also commenting at this place.

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>