开发者

how to test row locked exception in junit

Is there a way in junit to test rowlo开发者_如何学编程cked exception?


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


Something like this:

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


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.


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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜