I am creating some junit test cases using EasyMock. So far it makes sense for classes I am expecting to return POJOs, but how should I handle dealing with a DAO object that itself could throw an exception. My test case is to check for an expected exception thrown when the DAO encounters an issue. Using EasyMock I try and mock the DAO object (testing from the foo
class), what is the correct way to handle the lower level DAO exception.
An example of the classes/simple calls is 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 and test the foo
class, I want to be able to test for this DBException. How do I handle this, should I be surronding the DAO call in a try/catch (in the test), or add a throws to the test? I know expected=DBException
will pass the test if that is thrown, but how syntactically should you handle any number of inner exceptions?
Test code Example:
@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 the expect
could potentially throw errors? Should the test method throw
the exception, or should a try/catch
be used? Is it still correct to use the expected=EXCEPTION
tag on the test?