Pregunta

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 ?

¿Fue útil?

Solución 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.

Otros consejos

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top