2016-06-12 52 views
0

我正在尝试测试我的Play应用程序控制器,但我正在努力寻找让测试在使用控制器实例的同时返回两个会话数据。使用会话和控制器实例测试POST请求。

public class HomeControllerTest extends WithApplication { 

    @Override 
    protected Application provideApplication() { 
     return new GuiceApplicationBuilder() 
      .configure("play.http.router", "router.Routes") 
      .build(); 
    } 

    @Test 
    public void testGameChoiceHvH() { 
     Map form = new HashMap<String, String>(); 
     form.put("gameType", "0"); 
     HomeController homeController = new HomeController(); 
     Result result = invokeWithContext(fakeRequest().bodyForm(form), 
      () -> homeController.chooseGame()); 
     assertEquals(SEE_OTHER, result.status()); 
     assertEquals("/play", result.header("Location").get()); 
     assertFalse(result.session().isEmpty()); 
     String gameId = result.session().get("game_id"); 
     assertTrue(homeController.getGame(gameId).getCurrentPlayer() instanceof Human); 
    } 

} 

这里的最后断言模拟采取存储在会话中的游戏ID,并用它获取游戏实例,在控制器动作创建并存储在控制器实例中的地图。问题是,通过使用invokeWithContext,result完全不包含Cookie对象,因此无法检索。

另一种方法我发现创建一个POST请求如下:

public class HomeControllerTest extends WithApplication { 

    @Override 
    protected Application provideApplication() { 
     return new GuiceApplicationBuilder() 
      .configure("play.http.router", "router.Routes") 
      .build(); 
    } 

    @Test 
    public void testGameChoiceHvH() { 
     Map form = new HashMap<String, String>(); 
     form.put("gameType", "0"); 
     HomeController homeController = new HomeController(); 
     Result result = route(fakeRequest(routes.HomeController.chooseGame()).bodyForm(form)); 
     assertEquals(SEE_OTHER, result.status()); 
     assertEquals("/play", result.header("Location").get()); 
     assertFalse(result.session().isEmpty()); 
     String gameId = result.session().get("game_id"); 
     assertTrue(homeController.getGame(gameId).getCurrentPlayer() instanceof Human); 
    } 

} 

然而,这显然意味着,最终的断言看的HomeController的新实例,而不是一个使用因此,Map是空的。

为了清楚起见,这里是有关控制器代码:

public class HomeController extends Controller { 

    private WebInterface ui = new WebInterface(); 
    private Map<String, Board> boardMap = new HashMap<>(); 
    private Map<String, Game> gameMap = new HashMap<>(); 

    public Game getGame(String gameId) { 
     return gameMap.get(gameId); 
    } 

    public Result chooseGame() { 
     Map<String, String[]> request = request().body().asFormUrlEncoded(); 
     Board board = new Board(); 
     Game game = new Game(new PlayerFactory(ui).create(GameType.values()[Integer.valueOf(request.get("gameType")[0])])); 
     boardMap.put(Integer.toString(board.hashCode()), board); 
     gameMap.put(Integer.toString(game.hashCode()), game); 
     session("game_id", Integer.toString(game.hashCode())); 
     session("board_id", Integer.toString(board.hashCode())); 
     return redirect("/play"); 
    } 

任何帮助将非常感激。

回答

0

于是,经过太长时间寻找一个解决方案,这一点,我发现在源代码中的答案是:在此基础上

/** 
* Calls a Callable which invokes a Controller or some other method with a Context 
*/ 
public static <V> V invokeWithContext(RequestBuilder requestBuilder, Callable<V> callable) { 
    try { 
    Context.current.set(new Context(requestBuilder)); 
    return callable.call(); 
    } catch (Exception e) { 
    throw new RuntimeException(e); 
    } finally { 
    Context.current.remove(); 
    } 
} 

,信息,现在是显而易见的,为什么invokeWithContext不公开的情况下运行后。因此,作为一个快速和肮脏的实施解决方案:

@Test 
public void testGameChoiceHvH() { 
    Map form = new HashMap<String, String>(); 
    form.put("gameType", "0"); 
    HomeController homeController = new HomeController(); 

    Http.RequestBuilder request = fakeRequest(routes.HomeController.chooseGame()).bodyForm(form); 
    try { 
     Http.Context.current.set(new Http.Context(request)); 
     homeController.chooseGame(); 

    } catch (Exception e) { 
     throw new RuntimeException(e); 
    } finally { 

     assertFalse(Http.Context.current().session().isEmpty()); 
     String gameId = Http.Context.current().session().get("game_id"); 

     assertTrue(homeController.getGame(gameId).getCurrentPlayer() instanceof DelayedComputer); 

     Http.Context.current.remove(); 
    } 
} 

这暴露了双方的会话,HomeController的情况下,这样的断言都可以进行测试。