2011-08-07 60 views
0

我正在尋找一種在功能測試期間設置會話數據的方法。我想測試註銷操作。在功能測試期間設置會話數據

理想情況下,我願做這樣的事情

@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)); 
} 

Unfortunaly,當調用這個測試中,我得到了以下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) 
+0

哪一行是'ApplicationTest.testLogout(ApplicationTest.java:26)' – niels

回答

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); 
} 

我希望通過這些信息你可以解決你的問題。對不起,我沒有給你一個正確的解決方案,但現在我鬆了很多時間解決其他人的問題: -/

+0

剛剛嘗試過,我還是得到了一個空指針:(( – plcstpierre

+0

)但是你用它作爲一個普通的JUnit-Test而不是Functionaltest?我無法想象哪裏可能有NULL。 – niels

0

那麼,Play應該是一個無狀態的應用服務器。因此,在會話中存儲數據可能不是一個好的處理方法。

+0

好吧...但是我怎樣才能測試我的註銷功能? – plcstpierre

+0

那麼,由於通常需要登錄來訪問私人頁面,您可以「註銷」並嘗試訪問禁止頁面。如果您被拒絕,您的註銷將起作用。 –

+0

嗯..我認爲,許多Play應用程序使用會話來存儲一些用戶ID /令牌或類似的東西 – Touko

相關問題