tags:

views:

146

answers:

4

Is there a way in junit to test rowlocked exception?

+4  A: 

Something like this:

@Test(expected=SQLException.class)
public void testReadLockedRowException() {
    ...
}
Georgy Bolyuba
+3  A: 

Georgy is correct, but you'll need more in the implementation: two clients to access the database. One to read and hang onto the row, another to try and access it and generate the exception.

duffymo
I bet you do not need two threads for that. Two transactions should be enough. (Two connections for transaction-per-connection scenario)
Georgy Bolyuba
+6  A: 

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:

Noel M
+1  A: 

With mock objects, throw row locked exception from your mock DB.

With a real DB:

  1. Open TWO DB sessions
  2. In session 1 lock interesting row
  3. In session 2 access locked row in a conflicting mode without unlocking it before in session 1.
gpeche