在功能测试期间设置会话数据

时间:2011-08-07 21:04:18

标签: java playframework

我正在寻找一种在功能测试期间设置会话数据的方法。我想测试一个注销动作。

理想情况下,我想做类似的事情

    @Test
    public void testLogout() {
        Scope.Session session = Scope.Session.current();
        session.put(Application.SESSION_USER_ID, "1");

        Response response = GET("/application/logout");

        assertEquals(null, session.get(Application.SESSION_USER_ID));
}

不幸的是,当我调用此测试时,我得到以下NullppointerException。

java.lang.NullPointerException
    at ApplicationTest.testLogout(ApplicationTest.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at play.test.PlayJUnitRunner$StartPlay$1$1$1.execute(PlayJUnitRunner.java:73)
    at play.Invoker$Invocation.run(Invoker.java:265)
    at play.Invoker.invokeInThread(Invoker.java:67)
    at play.test.PlayJUnitRunner$StartPlay$1$1.evaluate(PlayJUnitRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at play.test.PlayJUnitRunner.run(PlayJUnitRunner.java:48)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:199)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

2 个答案:

答案 0 :(得分:0)

好吧,Play应该是无状态应用程序服务器。因此,在会话中存储数据可能不是一个好的方法。

答案 1 :(得分:0)

我不确定为什么功能测试会失败,但我可以解释一下我将如何测试它:

我进行了正常的单位测试:

@Before
public void setUp() {
    Session.current.set(new Session());
    Request.current.set(new Request());
    Response.current.set(new Response());
}

然后就是那样

@Test
public void testLogout() {
    Session.current().put(SESSION_KEY_USERID, "1");
    Response.current().setCookie(COOKIE_NAME, "A value");
    assertNotNull(Session.current().get(SESSION_KEY_USERID));
    assertTrue(Response.current().cookies.containsKey(COOKIE_NAME));
    Cookie cookie = Response.current().cookies.get(COOKIE_NAME);
    assertFalse(Integer.valueOf(0).equals(cookie.maxAge));
    assertEquals("A value", cookie.value);
    service.logout();
    assertNull(Session.current().get(SESSION_KEY_USERID));
    cookie = Response.current().cookies.get(COOKIE_NAME);
    assertTrue(Integer.valueOf(0).equals(cookie.maxAge));
    assertEquals("", cookie.value);
}

我希望通过这些信息可以解决您的问题。对不起,我没有给你一个正确的解决方案,但目前我在解决其他人的问题上花了很多时间: - /