我想知道如何编写单元测试以获得覆盖下面方法的catch块。 FOM.create(data)是一种静态方法。如何测试异常与Junit一起抛出
public String getValue(Data data) {
try {
return FOM.create(data);
} catch (UnsupportedEncodingException e) {
log.error("An error occured while creating data", e);
throw new IllegalStateException(e);
}
}
目前,这是我的单元测试,但没有击中catch块:
@Test (expected = UnsupportedEncodingException.class)
public void shouldThrowUnsupportedEncodingException() {
doThrow(UnsupportedEncodingException.class).when(dataService).getUpdatedJWTToken(any(Data.class));
try {
dataService.getValue(data);
}catch (IllegalStateException e) {
verify(log).error(eq("An error occured while creating data"), any(UnsupportedEncodingException.class));
throw e;
}
}
代码中的getUpdatedJWTToken在哪里? – Plog