Testing Java file upload with bodyRaw
is resulting in a null body
#12518
-
Hi! I'm updating my app to allow users to upload a JSON file. Here's my application code: public class AdminImportController {
public Result importProgram(Http.Request request) {
Http.MultipartFormData<Files.TemporaryFile> body = request.body().asMultipartFormData();
if (body == null) {
// The test below will trigger this block, even though it shouldn't
return ok(adminImportView.render("Request did not contain a file."));
}
Http.MultipartFormData.FilePart<Files.TemporaryFile> uploadedFile = body.getFile("file");
if (uploadedFile == null) {
return ok(adminImportView.render("No file was uploaded."));
}
Files.TemporaryFile uploadedFileRef = uploadedFile.getRef();
// ... file processing ...
return ok(adminImportView.render(fileContent));
}
} I'm trying write a test for this following the guidelines in https://www.playframework.com/documentation/2.9.x/JavaFileUpload#Testing-the-file-upload: public class AdminImportControllerTest {
private static final String TEST_FILE_CONTENT = "{ \"id\" : 32, \"adminName\" : \"email-program\", \"adminDescription\" : \"\"}";
@Test
public void importProgram_resultHasFileContent() throws IOException {
File file = getFile();
Http.MultipartFormData.Part<Source<ByteString, ?>> part =
new Http.MultipartFormData.FilePart<>(
"file",
"test.json",
Http.MimeTypes.JSON,
FileIO.fromPath(file.toPath()),
Files.size(file.toPath()));
Http.RequestBuilder request =
addCSRFToken(Helpers.fakeRequest()
.uri(routes.AdminImportController.importProgram().url())
.method("POST")
.bodyRaw(
Collections.singletonList(part),
play.libs.Files.singletonTemporaryFileCreator(),
app.asScala().materializer()));
Result result = controller.importProgram(request.build());
assertThat(result.status()).isEqualTo(OK);
assertThat(contentAsString(result)).contains(TEST_FILE_CONTENT);
}
private File getFile() {
String filePath = "/tmp/output.json";
try {
FileWriter file = new FileWriter(filePath);
file.write(TEST_FILE_CONTENT);
file.close();
return new File(filePath);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
} but I found that Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi Caitlin, your test code is wrong, you mix Play Scala code with Play Java code. This is what I changed: mkurz/playframework-discussion-12518@259ffaf
Please let me know if things work now for you. |
Beta Was this translation helpful? Give feedback.
-
Thanks for your assistance and time in reproducing and fixing the problem!! Re Play Scala vs Play Java: Our application is actually set up to use the We actually decided to tackle this a different way by having the user paste the text into a text box instead of uploading a file (civiform/civiform#7096), so this is now irrelevant for us. But I really appreciate your time in helping us to get this working. |
Beta Was this translation helpful? Give feedback.
Hi Caitlin,
your test code is wrong, you mix Play Scala code with Play Java code.
I set up a repo with your exact code to reproduce the problem, see the commits here: https://github.com/mkurz/playframework-discussion-12518/commits/main/
The second commit adds your code above.
The third commit makes the test work: If you clone the repo and run
sbt test
you will see it will succeed.This is what I changed: mkurz/playframework-discussion-12518@259ffaf
adminImportView
so I had to work around that.