Question

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 ?

Was it helpful?

Solution 2

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.

OTHER TIPS

Don't use a static block. Use a method tagged with @Before instead, and tack throws Exception onto its declaration.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top