【发布时间】:2018-11-30 20:28:30
【问题描述】:
我有一个测试 jaxbexception 的方法,在这个方法之前有初始验证,所以我不能发送无效请求来发生 JAXBException。我看到的唯一方法是使用 MOCKITO,尝试过但对我不起作用。
待测方法:
private String methodtoBeTested(Object request, Class clazz)
{
try
{
StringWriter sw = new StringWriter();
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.marshal(request, sw);
return sw.toString();
}
catch(JAXBException e)
{
LOG.error("jaxbexception encountered" + e.getMessage());
}
}
到目前为止我试过了:
JAXBContext jaxbMock = mock(JAXBContext.class);
Marshaller marshalmock = mock(Marshaller.class);
when(jaxbMock.createMarshaller()).thenThrow(JAXBException.class);
【问题讨论】: