HTTP context for testing in Java Play! 2.0.2

We are trying to write tests for our views, but some of them require session variables for proper visualization. Here's what the standard test looks like:

@Test public void indexTest() { running(fakeApplication(), new Runnable() { public void run() { Content html = views.html.index.render(loginForm); assertThat(contentType(html)).isEqualTo("text/html"); assertThat(contentAsString(html)).contains("log in"); } }); } 

loginForm is the layout we declared in the test class.

However, when we try to run this test, we get the following error:

"HTTP context is not available here"

We tried to use testServer and tried to get the HTTP context from requests to this server, but to no avail.

Thanks: -)

Edit @nico_ekito

This is the code surrounding my loginForm:

 Form<Login> loginForm = Controller.form(Login.class); 

However, I believe that the problem is in calling the controller, because the view does not use any session properties. The authenticate () method (in the controller that is being rendered, I believe when the form is submitted to the view), however, uses sessions.

+4
source share
1 answer

It would be helpful if you posted the whole view, because you could use the plugin / i18n plugin for auth or something like that, in turn, uses session properties. You can also try to mock the context with Mocktio or similar before running the test.

 Context.current.set( new Context(mock(Request.class), new HashMap<String, String>(), new HashMap<String, String>())); 

I would recommend going over to your views again by checking the properties of the session.

0
source

All Articles