title: Play testing NPE at play.core.j.PlayMagicForJava$.implicitJavaMessages
body: <p>I'm trying to get a Java test to work.
  It should only tests if a form is opened correctly.</p>
  
  <p>I'm using dependency injection through out my project.
  Therefore I create the controller via dependency injection.</p>
  
  <p>For the form I use a scala.html view. In this view the <code>@Messages()</code> is used.
  If I understand the error correct, it seems that this scala Message call doesn't work in my test.</p>
  
  <p><strong>What do I have to do so that <code>@Messages()</code> in scala.html views works in tests?</strong></p>
  
  <p>Error message:</p>
  
  <pre><code>[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]     ...
  </code></pre>
  
  <p>Test class:</p>
  
  <pre><code>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());
      }
  }
  </code></pre>