Is there a junit way to check out the recreation service that accepts MediaType.MULTIPART_FORM_DATA to download files?

I have a quiet web service that Essentailly downloads a file. However, I do not have a user interface created for it. I want to write a junit test that will call a service and download a file. How should I do it? I see no way to manually create a form to go to the service.

Here is my rest service:

@POST @Consumes(MediaType.MULTIPART_FORM_DATA) public Response uploadFile( @FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail) { String uploadedFileLocation = "c://uploadedFiles/" + fileDetail.getFileName(); // save it // saveToFile(uploadedInputStream, uploadedFileLocation); String output = "File uploaded via Jersey based RESTFul Webservice to: " + uploadedFileLocation; return Response.status(200).entity(output).build(); } 
+4
source share
2 answers

I implemented the following code and it works great. Let me know if you have a problem:

 FormDataMultiPart form = new FormDataMultiPart(); URI uri = new File("filepath").toURI(); InputStream data = this.getClass().getResourceAsStream("filePath"); FormDataBodyPart fdp1 = new FormDataBodyPart("key1", uri.toString()); FormDataBodyPart fdp2 = new FormDataBodyPart("key2",data, MediaType.APPLICATION_OCTET_STREAM_TYPE); form.bodyPart(fdp1).bodyPart(fdp2); response = builder.post(ClientResponse.class, form); Assert.assertEquals(response.getStatus(), Status.OK.getStatusCode()); 
+3
source

With JUnit, you can use the Jersey test platform .

0
source

Source: https://habr.com/ru/post/1500996/


All Articles