i migrating java play application 2.37 -> 2.4.1. in controller unit tests, had set controller along associated mocked dependencies before each test.
it seems getcontrollerinstance method removed globalsettings in play 2.4 can't override return controller instance.
@runwith(mockitojunitrunner.class) public class publicroomscontrollertest extends withapplication { @mock private myservice myservice; private mycontroller mycontroller; @before public void setup() { mycontroller = new mycontroller(myservice); globalsettings global = new globalsettings() { public <t> t getcontrollerinstance(class<t> clazz) { return (t) controller; } }; start(fakeapplication(global)); } @test public void mytest() { result result = route(new requestbuilder().method(post).uri("/test")); assertequals(ok, result.status()); } } i know can call method directly on controller instance test such as:
result result = mycontroller.somemethod(); assertequals(ok, result.status()); this approach seems work fine until somemethod() relies on form data in request like
map<string, string> data = form.form().bindfromrequest().data(); is there someway test route requests, may include form data, use controller instance?
(i using guice, mockito, , junit)
as pointed out me here https://github.com/playframework/playframework/issues/4876. correct way in play 2.4.x use helpers.invokewithcontext. test controller mocked dependencies used following code:
requestbuilder requestbuilder = new requestbuilder().bodyform(immutablemap.of("userid", 1)); result result = new helpers().invokewithcontext(requestbuilder, () -> mycontroller.somemethod()); side note: beleive invokewithcontext being changed static method in future.
Comments
Post a Comment