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.

12,433 thoughts on “Integration testing transactions and optimistic locking with Spring and JUnit

  1. Thank you for the good writeup. It in fact was a amusement account it.
    Look advanced to far added agreeable from you! By the way, how can we communicate?

  2. Hello there, You have done an incredible job. I will certainly digg
    it and personally recommend to my friends. I am confident they will
    be benefited from this website.

  3. Hello very cool blog Guy .. Excellent .. Superb .. Ill bookmark your blog and take the feeds also? I am satisfied to seek out so many useful information here in the put up wed like work out extra techniques in this regard thanks for sharing. . . . . . Candy Blitz Bombs

  4. Very good website you have here but I was curious about if you knew of any forums that cover
    the same topics talked about in this article? I’d really love to be a part
    of online community where I can get responses from other knowledgeable people that share the same interest.
    If you have any recommendations, please let me know. Bless you!

  5. That is very fascinating You are a very professional blogger. I have joined your rss feed and look forward to searching for extra of your great post. Additionally I have shared your web site in my social networks Candy Blitz Bombs

  6. I do not even know how I ended up here, but I thought
    this post was great. I do not know who you are but definitely you’re going to a famous blogger if you are not already ;) Cheers!

  7. Players who request self-exclusion must also report any other accounts and must not open new ones during
    the exclusion period.

  8. Hi my loved one! I wish to say that this article is amazing, great written and include approximately all significant infos.
    I’d like to see extra posts like this .

  9. Зацепил материал про Актуальные события и жизнь Липецкой области сегодня.

    Вот, делюсь ссылкой:

    https://48media.ru

  10. Hi there, i read your blog from time to time and i own a similar one and
    i was just wondering if you get a lot of spam remarks?
    If so how do you reduce it, any plugin or anything you can suggest?
    I get so much lately it’s driving me insane so any help is very much appreciated.

  11. bayar bayar bayar bayar bayar bayarbayar bayar bayar
    bayar bayar bayarbayar bayar bayar bayar bayar bayar bayar bayar
    bayar bayar bayar bayarbayar bayar bayar bayar bayar bayarbayar bayar bayar
    bayar bayar bayar bayar bayar bayar bayar bayar bayarbayar bayar bayar bayar bayar
    bayarbayar bayar bayar bayar bayar bayar bayar bayar bayar bayar bayar bayarbayar bayar bayar bayar
    bayar bayar

  12. Howdy! This is my first comment here so I just wanted to give a quick shout out and tell you I truly
    enjoy reading your posts. Can you suggest any other
    blogs/websites/forums that go over the same subjects?
    Appreciate it!

  13. I blog quite often and I seriously thank you for your content.
    The article has really peaked my interest.
    I will bookmark your website and keep checking for new
    information about once a week. I subscribed to your
    Feed too.

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>