EasyMock exception handling

I am creating some junit test cases using EasyMock. So far this makes sense for classes that I expect to return a POJO, but how should I handle a DAO object that itself may throw an exception. My test case is to check for the expected exception caused by the DAO encountering a problem. Using EasyMock, I am trying to make fun of a DAO object (testing from the foo class), which is the correct way to handle a lower level DAO exception.

An example of classes / simple calls is given below: (Assume all getters / setters / constructors are valid and present)

 public class foo{ private daoClass dao = daoClass.getInstance(); public String getValueFromDB(String key) throws DBException{ return dao.lookup(key); } } public class daoClass{ //singleton DAO public daoClass getInstance(){ //singleton access here } public String lookup(String key) throws DBException{ try{ //DB LOGIC } catch(Exception e){ throw new DBException(e.getMessage()); } } } 

When I try to check the foo class, I want to check this DBException. How can I handle this, should I be responsible for calling the DAO in try / catch (in the test) or add throws to the test? I know that expected=DBException will pass the test if it is thrown, but how should you syntactically handle any number of internal exceptions?

Sample test code:

 @Test(expected=DBException.class) public void testFooError(){ String key = "test"; String value = "expected"; daoClass daoMock = createMock(daoClass.class); try{ expect(daoMock.lookup(key)).andReturn(value); } catch (DBException e){ // ??? } } 

What is the correct way to handle when expect could potentially throw errors? Should throw test method be excluded or use a try/catch ? Is the expected=EXCEPTION tag used correctly in the test?

+4
source share
2 answers

This is how I handle exceptions in unit tests:

If you do not explicitly check the exception, you should add a throw clause for the method - if the exception was thrown and you did not expect its throw, then the test failed. eg.

 @Test public void testFooNormal() throws DBException{ String key = "test"; String value = "expected"; daoClass daoMock = createMock(daoClass.class); expect(daoMock.lookup(key)).andReturn(value); // do an assert on returned value ... } 

If you explicitly check for an exception, add a try-catch around the line from which you expect it to be selected (select the narrowest version of Exception that you expect), and then set the boolean value in the catch clause and approve it should be boolean. eg.

 @Test public void testFooError(){ String key = "test"; String value = "expected"; boolean exceptionThrown = false; daoClass daoMock = createMock(daoClass.class); try{ expect(daoMock.lookup(key)).andReturn(value); }catch (DBException e) { exceptionThrown = true; } // assert exceptionThrown is true ... } 

This is a good way to check for exceptions because it means that you are checking not only that the correct exception has been selected, but also that it is thrown from exactly the line you expect. If you use @test (expected = ...), then another line in the test may throw this exception, and the test may fail.

+5
source

Your DAO logic should not change depending on your test. If you expect your DAO to throw an exception, save it this way and test the Exception in the way you are testing.

If you catch an exception in the DAO itself to perform rollback or logging, etc., then the test case should not expect an exception, as this is not a script script. You can check for NULL in this case, as I expect your DAO to return NULL.

+3
source

All Articles