2011-08-07 2 views
0

기능 테스트 중에 세션 데이터를 설정하는 방법을 찾고 있습니다. 로그 아웃 작업을 테스트하고 싶습니다.기능 테스트 중에 세션 데이터 설정

이상적으로, 나는이 테스트를 호출 할 때, 나는 다음과 같은 NullppointerException있어, 그

@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 같은 일을하고 싶습니다.

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

기능 테스트가 실패하는 이유는 확실 해요,하지만 난 그것을 테스트 할 방법을 설명 할 수 있습니다 : 그

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

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

하고 뭔가 :

내가 보통의 유닛 테스트를했다

이 정보로 문제를 해결할 수 있기를 바랍니다. 미안하지만 올바른 해결책을주지는 못하지만 다른 사람들의 문제를 해결하는 데 많은 시간을 할애해야합니다. -/

+0

그냥 시도해 보았습니다. 아직 NullPointer가 있습니다. (( – plcstpierre

+0

하지만 Functionaltest가 아닌 일반적인 JUnit-Test로 사용합니까?) NULL이있을 수있는 곳을 상상할 수 없습니다. – niels

0

음, Play는 상태 비 저장 응용 프로그램 서버로 간주됩니다. 따라서 세션에 데이터를 저장하는 것이 좋은 방법이 아닙니다.

+0

줄 알았지 만 어떻게 내 로그 아웃 기능을 테스트 할 수 있습니까? – plcstpierre

+0

글쎄, 로그인은 일반적으로 개인 페이지에 액세스해야하기 때문에 "로그 아웃"하고 금지 된 페이지에 액세스하려고 할 수 있습니다. 거부 된 경우 로그 아웃이 작동합니다. –

+0

흠 .. 많은 Play 애플리케이션이 세션을 사용하여 일부 사용자 ID/토큰 또는 비슷한 것을 저장한다고 생각합니다. – Touko