java - Play route/Controller just for unit test -


i have method takes play http.context , "does stuff" session. want write unit test method. specifically, want test if request comes in headers method works correctly. seems easiest way reliably create fakeapplication , controller test. i'd use helpers.fakerequest request , helpers.route route request controller. controller call method, set variables, etc. , assert success , such.

seems splendid plan can't figure out how add route controller in fakeapplication. note controller isn't part of app - it's want use 1 test. want define , construct in 1 unit test; don't want add conf/routes file.

specifically, want this:

// maybe can use globalsettings.onrouterequest return type // play.api.mvc.handler seems inaccessible java fakeapplication app = helpers.fakeapplication(new myglobalsettings()); http.request request = helpers.fakerequest().withcookies(...).withbody(...); controller testcontoller = new mytestcontroller(); // doesn't exist, want app.addroute("/foo", ctx -> testcontroller.method(ctx)); running(app, () -> {     helpers.route("/foo");     assertthat(testcontoller.itworked()).istrue(); } 

i'm running play 2.2.3 , writing in java, not scala.

i realize can construct http.context directly , pass method. however, isn't preferred approach few reasons:

  • the http.context constructor takes plain text of session variables. want test things work correctly when request contains encrypted session cookie.
  • the http.context constructor poorly documented , seems bit off. example, can pass http.request constructor, pass cookie data , session data. happens cookie/session data on request? merged other data passed? ignored?
  • the http.context constructor difficult use java requires play.api.mvc.requestheader, can't constructed in java, , play.mvc.http.request can't "usefully" constructed java (you can construct one, without cookies, headers, etc. , fakerequest can't converted http.request).
  • it feels more "black box" send in request , ensure things work rather try figure out how particular version of play converts request http.context (e.g manually constructing context seems more break new versions of play).

any ideas?

play tests in format

running(fakeapplication(), () -> {   ... }); 

are testing running play app without http layer. in case you're dependent on having http context options either add in http layer...

running(testserver(3333), fakeapplication(), () -> {    wsresponse wsresponse = ws.url("http://localhost:3333/foo").setheader("fizz", "buzz").get().get(30, timeunit.seconds);   ....   //assert stuff }); 

or maybe try using powermockito , mock out http.context call. point out more brittle allow pragmatically spin quick unit test.

@runwith(powermockrunner.class) public class footest {      @preparefortest({ http.context.class })     @test     public void test() {         mockstatic(http.context.class)         mockstatic(http.class)          http.context mockcontext = mock(http.context.class);         map<string, string> args new hashmap<>();         args.put("a","b");         mockcontext.args = args;         powermockito.when(http.context.current()).thenreturn(mockcontext);          classundertest cut = new classundertest();         cut.somemethod();         //assertions      }  } 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -