문제

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 ?

도움이 되었습니까?

해결책 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.

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top