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");
}
任何幫助將非常感激。