0

I am trying to write a unit test for a method which uses: @Transactional. There are two methods are being called, each is running an update query on two table, if one fails other should not run or rollback.

I need to write a unit test to check if the transactional is working fine or not.

Please assist. Thanks in advance.

Nitin Kumar
  • 23
  • 1
  • 7

2 Answers2

0

mock second call throw exception close transaction check if data was removed

MarcinL
  • 130
  • 1
  • 6
0

testing the failure condition is not a bad thing, in fact it's something that should be done.

I would do integration tests for this, because you want to check a real database to see if the failure is rolled back as expected.

You have two tests :

  1. first update fails, everything should stop and second update should not run.
  2. first update works, second fails, so first update should be rolled back.

If you want unit tests, then use an in-memory database for example.

You don't say which database you are using, but typically you can check if an update succeeds as an update statement will tell you how many rows have been changed. If you get zero, or there is something wrong with the update statement and you get an error message, then you know you need to stop

Andrei Dragotoniu
  • 6,155
  • 3
  • 18
  • 32