【发布时间】:2016-06-16 04:39:18
【问题描述】:
我正在做类似于中提到的事情 Example of using StreamingOutput as Response entity in Jersey
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response streamExample(@Context UriInfo uriInfo) {
StreamingOutput stream = new StreamingOutput() {
@Override
public void write(OutputStream os) throws IOException,WebApplicationException {
try{
Writer writer = new BufferedWriter(new OutputStreamWriter(os));
//Read resource from jar
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("public/" + uriInfo.getPath());
...//manipulate the inputstream and build string with StringBuilder here//.......
String inputData = builder.toString();
Writer writer = new BufferedWriter(new OutputStreamWriter(os));
writer.write(inputData);
writer.flush();
} catch (ExceptionE1) {
throw new WebApplicationException();
}
}
};
return Response.ok(stream,MediaType.APPLICATION_OCTET_STREAM).build();
}
我正在尝试通过像How to get instance of javax.ws.rs.core.UriInfo 中提到的那样模拟 URIInfo 来对此进行单元测试
public void testStreamExample() throws IOException, URISyntaxException {
UriInfo mockUriInfo = mock(UriInfo.class);
Mockito.when(mockUriInfo.getPath()).thenReturn("unusal-path");
Response response = myresource.streamExample(mockUriInfo);}
当我将 jar 的路径切换到其他东西时,我希望能够检查我是否得到了一个异常。但是,当我运行/调试测试时,我从不进入
public void write(OutputStream os) throws IOException,
WebApplicationException {...}
部分,我只打return Response.ok(stream,MediaType.APPLICATION_OCTET_STREAM).build();
我在这里遗漏了一些非常明显的东西吗?
【问题讨论】:
标签: unit-testing jar jersey mockito junit4