如何使用 jersey / dropwizard 对流下载进行单元测试?
How do I unit test streaming download using jersey / dropwizard?
我有一个生成流式下载的资源方法:
@GET
@Path("/{assetId}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response download(@PathParam("assetId") String assetId) {
StreamingOutput stream = os -> service.download(assetId, os);
return Response.ok(stream).build();
}
我想用模拟服务对象对此进行单元测试。我已经有:
private static AssetsService service = Mockito.mock(AssetsService.class);
@ClassRule
public final static ResourceTestRule resource = ResourceTestRule.builder()
.addResource(new AssetsResource(service))
.addProvider(MultiPartFeature.class)
.build();
@Test
public void testDownload() {
reset(service);
// how to get an output stream from this?
resource.client().target("/assets/123").request().get();
}
根据我在测试中的评论,我需要做什么才能从响应中获取输出流?我发现球衣客户端 API 非常混乱。
一旦我有了这个,我将存根服务调用,以便它写入一个已知文件,并测试它是否被正确接收。
试试这个:
Response response = resource.client().target("/assets/123").request().get();
InputStream is = response.readEntity(InputStream.class);
我有一个生成流式下载的资源方法:
@GET
@Path("/{assetId}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response download(@PathParam("assetId") String assetId) {
StreamingOutput stream = os -> service.download(assetId, os);
return Response.ok(stream).build();
}
我想用模拟服务对象对此进行单元测试。我已经有:
private static AssetsService service = Mockito.mock(AssetsService.class);
@ClassRule
public final static ResourceTestRule resource = ResourceTestRule.builder()
.addResource(new AssetsResource(service))
.addProvider(MultiPartFeature.class)
.build();
@Test
public void testDownload() {
reset(service);
// how to get an output stream from this?
resource.client().target("/assets/123").request().get();
}
根据我在测试中的评论,我需要做什么才能从响应中获取输出流?我发现球衣客户端 API 非常混乱。
一旦我有了这个,我将存根服务调用,以便它写入一个已知文件,并测试它是否被正确接收。
试试这个:
Response response = resource.client().target("/assets/123").request().get();
InputStream is = response.readEntity(InputStream.class);