Java – handling exceptions using mockito

I use mockito in unit tests I have a way

public Status getResponse(Request requset) throws DataException{
}

Dataexception is a self - defined that inherits from the 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 for mockito to deal with this problem without adding try / catch?

Solution

Add this to your test method:

@Test(expected=DataException.class)

Or use this:

then(caughtException()).isinstanceOf(DataException.class);

There is no alternative to try catch for static blocks

One way is to change dataexception to runtimeException

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>