我一直在尝试创建一个Http。未成功使用其构造函数的测试的上下文。有人知道我做错了什么吗?
我看过以下内容,但它仅适用于Play 2.0:
Play framework 2.0:在Http中存储值。语境
它看起来像是为2.2.1更改的类,并且它有更多构造函数的参数,如下所示:
https://github.com/playframework/playframework/blob/2.1.x/framework/src/play/src/main/java/play/mvc/Http.java
这是我的代码:
import java.util.Map;
import java.util.Collections;
import org.junit.*;
import static org.mockito.Mockito.*;
import play.mvc.*;
import play.test.*;
import play.mvc.Http;
import play.mvc.Http.Context;
import play.api.mvc.RequestHeader;
import static play.test.Helpers.*;
import static org.fest.assertions.Assertions.*;
public class TemplateTests {
public static FakeApplication app;
private final Http.Request request = mock(Http.Request.class);
@BeforeClass
public static void startApp() {
app = Helpers.fakeApplication();
Helpers.start(app);
}
@Before
public void setUp() throws Exception {
Map<String, String> flashData = Collections.emptyMap();
Map<String, Object> argData = Collections.emptyMap();
Long id = 2L;
play.api.mvc.RequestHeader header = mock(play.api.mvc.RequestHeader.class);
Http.Context context = mock(Http.Context(id, header, request , flashData, flashData, argData));
Http.Context.current.set(context);
}
@Test
public void renderTemplate() {
Content html = views.html.index.render();
assertThat(contentType(html)).isEqualTo("text/html");
assertThat(contentAsString(html)).contains("myindex");
}
@AfterClass
public static void stopApp() {
Helpers.stop(app);
}
}
这是我在运行测试时看到的错误:
play test
[info] Loading project definition from /home/user/solr-segmentexplorer/explorer/project
[info] Set current project to explorer (in build file:/home/user/solr-segmentexplorer/explorer/)
[info] Compiling 1 Java source to /home/user/solr-segmentexplorer/explorer/target/scala-2.10/test-classes...
[error] /home/user/solr-segmentexplorer/explorer/test/TemplateTests.java:33: cannot find symbol
[error] symbol : method Context(java.lang.Long,play.api.mvc.RequestHeader,play.mvc.Http.Request,java.util.Map<java.lang.String,java.lang.String>,java.util.Map<java.lang.String,java.lang.String>,java.util.Map<java.lang.String,java.lang.Object>)
[error] location: class play.mvc.Http
[error] Http.Context context = mock(Http.Context(id, header, request , flashData, flashData, argData));
[error] ^
[error] 1 error
[error] (test:compile) javac returned nonzero exit code
[error] Total time: 3 s, completed Nov 25, 2013 11:56:36 PM
有什么想法吗?
如果我不创造一个环境,我会得到:
[error]Test TemplateTest. renderTemplate失败:java.lang.RuntimeException:这里没有可用的HTTP上下文。
看起来这似乎已经为我修复了它:
@Before
public void setUp() throws Exception {
Map<String, String> flashData = Collections.emptyMap();
Map<String, Object> argData = Collections.emptyMap();
Long id = 2L;
play.api.mvc.RequestHeader header = mock(play.api.mvc.RequestHeader.class);
Http.Context context = new Http.Context(id, header, request, flashData, flashData, argData);
Http.Context.current.set(context);
}
具体修复它的部分是:
Http.Context.current.set(context);
只是为了提供一个使用Mockito的替代方案,只嘲笑你需要的东西(没有手动实例化任何类):
private Http.Context getMockContext() {
Http.Request mockRequest = mock(Http.Request.class);
when(mockRequest.remoteAddress()).thenReturn("127.0.0.1");
when(mockRequest.getHeader("User-Agent")).thenReturn("mocked user-agent");
// ... and so on. Mock precisely what you need, then add it to your mocked Context
Http.Context mockContext = mock(Http.Context.class);
when(mockContext.request()).thenReturn(mockRequest);
when(mockContext.lang()).thenReturn(Lang.forCode("en"));
return mockContext;
}
您还可以验证这些字段是否已被使用:
@Test
public void testMockContext() {
final Http.Context mockContext = getMockContext();
assertThat(mockContext.request()).isNotNull();
verify(mockContext).request();
final String remoteAddress = mockContext.request().remoteAddress();
assertThat(remoteAddress).isNotNull();
assertThat(remoteAddress).isEqualTo("127.0.0.1");
verify(mockContext.request()).remoteAddress();
}
不要忘记导入静态 org.mockito.Mockito.*
只是嘲笑上下文类解决了问题
@Before
public void setUp() throws Exception {
Http.Context context = mock(Http.Context.class);
Http.Context.current.set(context);
}