Search code examples
unit-testingjarjerseymockitojunit4

Unit Testing StreamingOuput as Response entity Jersey


I am doing something similar to mentioned in 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();
}

I am trying to unit test this by mocking URIInfo like mentioned in How to get instance of javax.ws.rs.core.UriInfo

  public void testStreamExample() throws IOException, URISyntaxException {
        UriInfo mockUriInfo = mock(UriInfo.class);
        Mockito.when(mockUriInfo.getPath()).thenReturn("unusal-path");
        Response response = myresource.streamExample(mockUriInfo);}

I want to be able to check that I get an Exception when I switch the path to jar to something else.But, when I run/debug the test, I never enter the

public void write(OutputStream os) throws IOException,
            WebApplicationException {...}

part and I only always hit the return Response.ok(stream,MediaType.APPLICATION_OCTET_STREAM).build();

Am I missing something very obvious here??


Solution

  • Because the stream is not written to until it hits the MessageBodyWriter (which is the component that ends up calling the StreamingOutput#write).

    What you can do, is just get the Response from the return and call Response#getEntity() (which returns an Object) and cast it to StreamingOutput. Then call the write method yourself, passing an OutputStream, maybe a ByteArrayOutputStream so you can get the contents as a byte[] to check it. It all would look something like

    UriInfo mockInfo = mockUriInfo();
    Response response = resource.streamExample(mockInfo);
    StreamingOutput output = (StreamingOutput) response.getEntity();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    output.write(baos);
    byte[] data = baos.toByteArray();
    String s = new String(data, StandardCharsets.UTF_8);
    assertThat(s, is("SomeCharacterData"));