Search code examples
jsonpostplayframeworkhttp-status-code-404functional-testing

WSRequest POST a JSON object in FunctionalTest would end up getting 404


I have the following test case that I want to post a piece of json data, but I am getting 404 now, am I doing something wrong here?

@Test
public void testIndex() {
    User user = new User("[email protected]", "secret", "test", "(111)111-1111");
    Gson gson = new Gson();
    String postJson = gson.toJson(user);

    // post to add the new user
    WSRequest postRequest = WS.url("http://localhost:9001/user/add");
    postRequest.body = postJson;
    HttpResponse postResponse = postRequest.post();
    assertEquals(postResponse.getStatus(), (Integer)200);
}

Solution

  • An HTTP 404 means either your path is not correct, or inside the method that receives the POST request in your controller you are trying to do a redirect to a page that doesn't exist.

    Seeing your code (direct check of status) I would bet on the first. Can you add the code of your routes file in here? And verify that Play is running in 9001, not 9000?