如何使用jersey / dropwizard对流下载进行单元测试?

时间:2017-03-16 19:59:13

标签: unit-testing 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非常混乱。

一旦我有了这个,我就会对服务调用进行存根,以便它编写一个已知文件,并测试它是否正确接收。

1 个答案:

答案 0 :(得分:1)

试试这个:

<?php
$function_name = mt_rand(100,10000); //rand name
$func_{$function_name} = function(){ 
    echo 'Your function';
}; //variable that contains the function, named with the rand value obtained 

echo "Name: func_$function_name() =>";
$func_{$function_name}(); //Execute function