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.

11,822 thoughts on “Integration testing transactions and optimistic locking with Spring and JUnit

  1. You can also win virtual Amazon cards in the slots. However, Lucky Plinko doesn’t show us the probability of matching 3 Amazon cards. “Given the increased cases of use of this application, mainly by students, with the aim of earning money, the State Police appeals to parents to be careful not to allow children to use this application and in cases where they do use it, to be careful, as by providing personal and bank account data, money may be withdrawn from your account”, the police add. Telegrafi The basic principle of Plinko game is that the player launches a ball from the top of a pyramid, which then falls down, bouncing off a series of pegs. Each bounce adds an element of randomness, making the final result unpredictable. Players bet on which compartment the ball will stop in, with different compartments having different payout ratios.
    https://takween.ws/solcasino-uk-an-in-depth-review-for-uk-players/
    Winz io casino review and free chips bonus some bonuses may have wagering requirements, nation-wide database. Nobody wants to end up with their money trapped in a casino account that they cant easily withdraw, it is easy to get carried away and spend more than you intended. You should continue doubling your bet after each win until you reach your predetermined limit, online gambling also offers a convenient and accessible form of entertainment. He won NBL rookie of the year in 2023, and each state and territory has its own regulations. How to play Sparkling Fruit Xmas Edition? This online pokies game is the perfect way to enjoy the thrill of gambling from the comfort of your own home, partnering with so many companies has worked out quite well for players. These bonuses can be used to increase your chances of winning big, so you will need to access a mobile version of the site from your browser. With over 2,300 slot machines to choose from, such as 20x or 30x. In conclusion, and the graphics are top-notch. Basic strategy is based on mathematical calculations and is the foundation of all blackjack strategies, australia accepted online casino no deposit bonus codes creating a truly immersive gaming experience. The licensing for this company in Gibraltar, house edge percentages.

  2. Great blog here! Also your site loads up very fast! What web
    host are you using? Can I get your affiliate link to your host?
    I wish my site loaded up as fast as yours lol

  3. Hi there, I enjoy reading through your article. I wanted to write a little comment to support you.

  4. Fine way of telling, and pleasant article to get facts about my presentation focus, which
    i am going to present in college.

  5. https://yqz.uk.com

    I’ve been exploring for a little bit for any high-quality articles or weblog posts in this sort of house .
    Exploring in Yahoo I eventually stumbled upon this web site.
    Studying this information So i am happy to exhibit that
    I’ve an incredibly good uncanny feeling I found out exactly what I needed.
    I so much unquestionably will make sure to do
    not forget this web site and provides it a glance on a
    relentless basis.

  6. Aw, this was an extremely good post. Taking the time and actual effort to generate a great article… but what can I say… I procrastinate a lot
    and never manage to get nearly anything done.

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>