2014-04-01 4 views
2

최신 documentation에서는이 템플릿 테스트를 예로 들어 설명합니다.PlayFramework 2 템플릿 테스트

@Test 
public void renderTemplate() { 
    Content html = views.html.index.render("Coco"); 
    assertThat(contentType(html)).isEqualTo("text/html"); 
    assertThat(contentAsString(html)).contains("Coco"); 
} 

하지만 어떻게해야합니까? 가짜 서버, 실제 서버, 실제 서버를 실행하는 run() 메서드 내부에서 자체적으로 시도했지만 항상이 오류가 발생합니다.

[error] Test ApplicationTest.testInServer failed: java.lang.RuntimeException: There is no HTTP Context available from here. 

문서에는 실제로 두 페이지의 테스트가 있으며 실제로 이러한 테스트를 실행하는 방법을 알지 못합니다. 더 이상 사용되지 않는 메소드를 사용하지 않는 곳에서 예제 클래스가 있습니까 (Play 1 이후에 변경된 사항이며 대부분 더 이상 작동하지 않습니다).

답변

0

명령 줄에서 "play test"를 실행하십시오. 그러면 테스트 폴더의 모든 테스트가 실행됩니다.

+0

내가 테스트를 실행하는 방법이다, 나는 내 게시물에 복사 오류가 발생했습니다. – user2787904

3

:

은 도움의 마지막 부분을 참조하십시오. Mockito와

예 :

@Test 
public void indexTest() { 
    //setup HTTP Context 
    Http.Context context = Mockito.mock(Http.Context.class); 
    //mocking flash session, request, etc... as required 
    Http.Flash flash = Mockito.mock(Http.Flash.class); 
    when(context.flash()).thenReturn(flash); 
    Http.Context.current.set(context); 

    //run your test 
    Content html = views.html.index.render("Coco"); 
    assertThat(contentType(html)).isEqualTo("text/html"); 
    assertThat(contentAsString(html)).contains("Coco"); 
}