Play framework 2.2.1: Create Http.Context for tests

后端 未结 4 1399
半阙折子戏
半阙折子戏 2020-12-29 10:35

I\'ve been trying to create an Http.Context for tests using its constructor unsuccessfully. Does anybody see what I\'m doing wrong?

I\'ve looked at the following but

4条回答
  •  悲哀的现实
    2020-12-29 11:01

    As a combination of the other answers:

    In build.sbt:

    libraryDependencies += "org.mockito" % "mockito-core" % "1.10.19" % "test"
    

    In your test class:

    import play.mvc.Http;
    import static org.mockito.Mockito.*;
    
    @Before
    public void setUp() throws Exception {
        Http.Context context = mock(Http.Context.class);
        Http.Flash flash = mock(Http.Flash.class);
        when(context.flash()).thenReturn(flash);
        Http.Context.current.set(context);
    }
    

    If you need more, just Mockito's functionalities. In case you are seeing any exceptions, just inspect the compiled code. IN my case, it was in target/scala-2.11/twirl/main/views/html/main.template.scala.

提交回复
热议问题