I'm trying to get a Java test to work. It should only tests if a form is opened correctly.
I'm using dependency injection through out my project. Therefore I create the controller via dependency injection.
For the form I use a scala.html view. In this view the @Messages() is used. If I understand the error correct, it seems that this scala Message call doesn't work in my test.
What do I have to do so that @Messages() in scala.html views works in tests?
Error message:
[error] Test RegistrationTest.openRegistrationForm failed: java.lang.NullPointerException: null, took 3.718 sec
[error] at play.core.j.PlayMagicForJava$.implicitJavaMessages(TemplateMagicForJava.scala:36)
[error] at views.html.registrationFormView$.apply(registrationFormView.template.scala:44)
[error] at views.html.registrationFormView$.render(registrationFormView.template.scala:79)
[error] at views.html.registrationFormView.render(registrationFormView.template.scala)
[error] at controllers.RegistrationController.registrationForm(RegistrationController.java:46)
[error] at RegistrationTest.openRegistrationForm(RegistrationTest.java:40)
[error] ...
Test class:
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static play.mvc.Http.Status.OK;
import static play.test.Helpers.contentAsString;
import org.junit.*;
import com.google.common.collect.ImmutableMap;
import controllers.RegistrationController;
import exceptions.message.ResultMessageException;
import play.mvc.*;
import play.test.WithApplication;
public class RegistrationTest
extends WithApplication {
private RegistrationController controller;
@Before
public void setUp() throws Exception {
controller = app.injector()
.instanceOf(RegistrationController.class);
Http.Request mockRequest = new Http.RequestBuilder().remoteAddress("127.0.0.1")
.header("User-Agent", "mocked user-agent")
.build();
Http.Context mockContext = mock(Http.Context.class);
when(mockContext.request()).thenReturn(mockRequest);
Http.Context.current.set(mockContext);
}
@Test
public void openRegistrationForm() {
Result result = null;
try {
result = controller.registrationForm();
} catch (ResultMessageException e) {
result = e.getResult();
}
assertEquals(OK, result.status());
assertEquals("text/html", result.contentType()
.get());
assertEquals("utf-8", result.charset()
.get());
}
}
Aucun commentaire:
Enregistrer un commentaire