java - Test POST request on PlayFramework 2.4.x -


good morning,

i'm trying test post requests on controllers.

i have no problems request :

@test public void testgetall() {     testmodel test = new testmodel();     test.done = true;     test.name = "pierre";     test.save();      testmodel test2 = new testmodel();     test2.done = true;     test2.name = "paul";     test2.save();      result result = new controllers.ressources.testressource().get(null);     assertequals(200, result.status());     assertequals("text/plain", result.contenttype());     assertequals("utf-8", result.charset());     asserttrue(contentasstring(result).contains("pierre"));     asserttrue(contentasstring(result).contains("paul")); } 

but when have test post request, can't give post params controller.

here method want test :

public result post() {     map<string, string> params =     requestutils.convertrequestforjsondecode(request().querystring());      t model = json.fromjson(json.tojson(params), generictype);     model.save();      reponse.setdata(model);     return ok(json.prettyprint(json.tojson(reponse))); } 

i've try several solutions, can't find proper 1 :

  • try use fakerequest
  • try mock http.request object

so, best way write tests controllers ?

i'm using play framework 2.4.6 java. junit 4 , mockito.

for tests of post action use requestbuilder , play.test.helpers.route method.

for 1 json data (i use jackson's objectmapper marshaling):

public class mytests {    protected application application;    @before   public void startapp() throws exception {     classloader classloader = fakeapplication.class.getclassloader();     application = new guiceapplicationbuilder().in(classloader)             .in(mode.test).build();     helpers.start(application);   }    @test   public void mypostactiontest() throws exception {      jsonnode jsonnode = (new objectmapper()).readtree("{ \"somename\": \"samevalue\" }");     requestbuilder request = new requestbuilder().method("post")             .bodyjson(jsonnode)             .uri(controllers.routes.mycontroller.myaction().url());     result result = route(request);      assertthat(result.status()).isequalto(ok);   }    @after   public void stopapp() throws exception {     helpers.stop(application);   } } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -