Reputation: 1636
I'm using Mockito
in my unit testing. I have a method
public Status getResponse(Request requset) throws DataException{
}
DataException
is my own defined one which inherited from Exception class.
In my test case
static{
when(process.getResponse(any(Request.class))).
thenReturn(new Status("Success"));
}
It gives an error, Unhandled Exception:DataException
Is there any way in Mockito
to handle this issue without adding try/catch ?
Upvotes: 10
Views: 15289
Reputation: 5414
add this to your test method:
@Test(expected=DataException.class)
or use this :
then(caughtException()).isInstanceOf(DataException.class);
for a static-block there is no way other than try-catch.
Another way is to change DataException
to a RuntimeException
.
Upvotes: 5
Reputation: 79808
Don't use a static
block. Use a method tagged with @Before
instead, and tack throws Exception
onto its declaration.
Upvotes: 8