1

I want to test my upload routine. But nothing works. Is there a way to test the upload of an MultipartFormData upload in Java?

My approaches: The first one, but that do not work...withFormURLEncodedBody only accepts a map.

    // generate fake cookie
    Cookie fakeCookie = Utils.fakeCookie(userInfo.getName());
    // create a fake request
    FakeRequest fakeRequest = new FakeRequest("POST", "/restricted/upload");
    // create an Hashmap for data
    Map<String, Object> data = new HashMap<String, Object>();
    File file = new File(
            RootPathConstant.TEST_FILES_PATH.concat(File.separator
                    .concat("test.nt")));
    data.put("file", file);
    // call an action with some parameters and receive results
    Result result = Helpers.callAction(controllers.routes.ref.UserOptions
            .uploadDataset(),fakeRequest.withFormUrlEncodedBody(data)
            .withCookies(fakeCookie));

My second approach ist like this:

Test MultipartFormData in Play 2.0 FakeRequest But it don't initiate any save actions after the post was executed. The site can be accessed but it seems that the post controlleraction was not called

Have anyone an idea to solve this?

Community
  • 1
  • 1
Lunero
  • 666
  • 1
  • 8
  • 17
  • This answer to a similar question might help: http://stackoverflow.com/a/28130543/1278769 – Kris Jan 24 '15 at 21:44

1 Answers1

0

I have solved this issue after trying some approaches.

The solution:

Because I'm using the securesocial 2 plugin I have written a login routine in java and then fetched the neccessary cookies, added them to a localcontext and then executed another request with that localcontext to send the file to the server.

That worked for me.

Lunero
  • 666
  • 1
  • 8
  • 17