0
我試圖讓Java測試工作。 它應該只測試一個表單是否正確打開。
我通過我的項目使用依賴注入。 因此我通過依賴注入來創建控制器。
對於我使用scala.html視圖的表單。在這個視圖中使用@Messages()
。 如果我明白錯誤是正確的,似乎這個scala消息調用在我的測試中不起作用。
我該怎麼做才能讓scala.html中的@Messages()
視圖在測試中起作用?
錯誤消息:
[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] ...
測試類:
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());
}
}