下面是一段代码。现在已经捕获了异常。我该如何写同样的阴性测试?确保代码进入catch块?
public ThirdPartyResponse load(ThirdPartyRequestContext context) {
ThirdPartyResponse thirdPartyResponse = new ThirdPartyResponse();
try
{
thirdPartyResponse.setData(getData(context));
}catch (Exception e)
{
LOGGER.error("Error executing {}", context.getEndpoint(), e);
thirdPartyResponse.setData(Collections.emptyList());
thirdPartyResponse.setErrorMessage(e.getMessage());
}
return thirdPartyResponse;
}
最佳答案
您可以断言从您的方法返回的thirdPartyResponse对象的错误消息不为空:
assertNotNull(thirdPartyResponse.getErrorMessage());
进口声明:
import static org.junit.Assert.assertNotNull;