Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to test row locked exception in junit

Tags:

java

junit

Is there a way in junit to test rowlocked exception?

like image 311
Dead Programmer Avatar asked Dec 08 '22 02:12

Dead Programmer


1 Answers

If you mock your database dependencies, then you can have your mock object throw the locked row exception.

As you'll have removed your direct dependency on the database, the test should run faster, and you'd be less prone to "flickering tests" due to database problems such as the database not being a available or something similar.

Also, this way, you are only testing your code, and nothing to do with the database - it is implementation agnostic. Should you choose to change database vendor in the future, this (a) shouldn't matter to your code and (b) your test doesn't care what database it is using.

Some example mocking frameworks to get you started:

  • EasyMock
  • Mockito
  • jMock
like image 174
Noel M Avatar answered Dec 26 '22 03:12

Noel M