How to check row lock exception in junit

Is there a way in junit to check for row-specific exceptions?

+4
source share
4 answers

If you mock your database dependencies, then you can make your layout object throw a locked row exception.

As you remove your direct dependency on the database, the test should run faster and you would be less prone to โ€œflickering testsโ€ due to problems with the database, such as a database that is not accessible or something like that.

In addition, in this way, you only check your code and have nothing to do with the database - this is implementation agnostic. If you decide to change the database provider in the future, (a) it does not matter for your code and (b) your test does not care which database it uses.

Some sample frameworks to get started:

+6
source

Something like that:

@Test(expected=SQLException.class) public void testReadLockedRowException() { ... } 
+5
source

George is right, but you need more to implement: two clients to access the database. One for reading and pasting on a line, another for accessing it and creating an exception.

+3
source

With mock objects, exclude row lock exception from your mock database.

With a real DB:

  • Open TWO DB Sessions
  • In session 1, lock an interesting row
  • In session 2, the access string is locked in conflicting mode without unlocking it before session 1.
+1
source

Source: https://habr.com/ru/post/1316332/


All Articles