Issue
So i've had some trouble with doing Unit test of a method that parse PDFs
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response parsingPdfOrange(@FormDataParam("fichier") InputStream uploadedInputStream,
@FormDataParam("fichier") FormDataContentDisposition fileDetail) {
try {
String uploadedFileLocation = "D:/tmp/doc.pdf";
String info = "facture Orange:"
+ OrangeService.ParseFacture(uploadedFileLocation, uploadedInputStream).toString();
return Response.status(201).entity(info).build();
which works fine my url give my the good result etc but
@Override
protected Application configure() {
return new ResourceConfig(OrangeWebService.class).register(MultiPartFeature.class);
}
@Override
protected void configureClient(ClientConfig config) {
config.register(MultiPartFeature.class);
}
@Test
public void parsePDF_NullPointerExcpetion() throws Exception {
try {
FileDataBodyPart filePart = new FileDataBodyPart("file", new File(pathtoRessources));
FormDataMultiPart formDataMultipart = new FormDataMultiPart();
FormDataMultiPart multipart = (FormDataMultiPart) formDataMultipart.bodyPart(filePart);
Response response = (target("pdf").request().post(Entity.entity(multipart, multipart.getMediaType())));
System.out.println(response);
response.close();
} catch (Exception e) {
e.printStackTrace();
}
this is my test code and the pb is here: it seems my FormDataMultiPart is fine but when i do Entity.entity(multipart, multipart.getMediaType() it gives back a null value because i get a nullPointerException on it.
I need help, Thanks a lot.
Solution
The file part name is "fichier"
@FormDataParam("fichier") InputStream uploadedInputStream
But you are sending it as "file"
FileDataBodyPart filePart = new FileDataBodyPart("file", new File(pathtoRessources));
Fix that.
Answered By - Paul Samsotha