diff options
author | jbion <joffrey.bion@amadeus.com> | 2019-02-21 01:26:36 +0100 |
---|---|---|
committer | jbion <joffrey.bion@amadeus.com> | 2019-02-21 01:26:36 +0100 |
commit | 6d83b6fada8942650566e6ed46ead95eb05954b9 (patch) | |
tree | 0219c5817f68c4ed1b0dcadb96e4092fc4d7e96d /backend/src/test/kotlin/org/luxons | |
parent | Remove non idiomatic usages of lists (diff) | |
download | seven-wonders-6d83b6fada8942650566e6ed46ead95eb05954b9.tar.gz seven-wonders-6d83b6fada8942650566e6ed46ead95eb05954b9.tar.bz2 seven-wonders-6d83b6fada8942650566e6ed46ead95eb05954b9.zip |
Replace expected exceptions with assertFailsWith
Diffstat (limited to 'backend/src/test/kotlin/org/luxons')
6 files changed, 113 insertions, 56 deletions
diff --git a/backend/src/test/kotlin/org/luxons/sevenwonders/controllers/GameBrowserControllerTest.kt b/backend/src/test/kotlin/org/luxons/sevenwonders/controllers/GameBrowserControllerTest.kt index 98506fd0..343b7f34 100644 --- a/backend/src/test/kotlin/org/luxons/sevenwonders/controllers/GameBrowserControllerTest.kt +++ b/backend/src/test/kotlin/org/luxons/sevenwonders/controllers/GameBrowserControllerTest.kt @@ -11,6 +11,7 @@ import org.luxons.sevenwonders.repositories.PlayerNotFoundException import org.luxons.sevenwonders.repositories.PlayerRepository import org.luxons.sevenwonders.test.mockSimpMessagingTemplate import kotlin.test.assertEquals +import kotlin.test.assertFailsWith import kotlin.test.assertFalse import kotlin.test.assertTrue @@ -54,15 +55,17 @@ class GameBrowserControllerTest { assertEquals(player.toDTO(principal.name), lobby.players[0]) } - @Test(expected = PlayerNotFoundException::class) + @Test fun createGame_failsForUnknownPlayer() { val principal = TestPrincipal("unknown") - val action = CreateGameAction("Test Game") - gameBrowserController.createGame(action, principal) + + assertFailsWith<PlayerNotFoundException> { + gameBrowserController.createGame(action, principal) + } } - @Test(expected = UserAlreadyInGameException::class) + @Test fun createGame_failsWhenAlreadyInGame() { playerRepository.createOrUpdate("testuser", "Test User") val principal = TestPrincipal("testuser") @@ -75,7 +78,9 @@ class GameBrowserControllerTest { val createGameAction2 = CreateGameAction("Test Game 2") // already in a game - gameBrowserController.createGame(createGameAction2, principal) + assertFailsWith<UserAlreadyInGameException> { + gameBrowserController.createGame(createGameAction2, principal) + } } @Test @@ -97,7 +102,7 @@ class GameBrowserControllerTest { assertEquals(joiner.toDTO(joinerPrincipal.name), joinedLobby.players[1]) } - @Test(expected = UserAlreadyInGameException::class) + @Test fun joinGame_failsWhenAlreadyInGame() { playerRepository.createOrUpdate("testowner", "Test User Owner") val ownerPrincipal = TestPrincipal("testowner") @@ -112,6 +117,8 @@ class GameBrowserControllerTest { // joins the game gameBrowserController.joinGame(joinGameAction, joinerPrincipal) // should fail because already in a game - gameBrowserController.joinGame(joinGameAction, joinerPrincipal) + assertFailsWith<UserAlreadyInGameException> { + gameBrowserController.joinGame(joinGameAction, joinerPrincipal) + } } } diff --git a/backend/src/test/kotlin/org/luxons/sevenwonders/controllers/LobbyControllerTest.kt b/backend/src/test/kotlin/org/luxons/sevenwonders/controllers/LobbyControllerTest.kt index 6e417970..a140e000 100644 --- a/backend/src/test/kotlin/org/luxons/sevenwonders/controllers/LobbyControllerTest.kt +++ b/backend/src/test/kotlin/org/luxons/sevenwonders/controllers/LobbyControllerTest.kt @@ -17,6 +17,7 @@ import org.luxons.sevenwonders.repositories.PlayerRepository import org.luxons.sevenwonders.test.mockSimpMessagingTemplate import java.util.HashMap import kotlin.test.assertEquals +import kotlin.test.assertFailsWith import kotlin.test.assertFalse import kotlin.test.assertSame import kotlin.test.assertTrue @@ -49,17 +50,23 @@ class LobbyControllerTest { assertFalse(owner.isInGame) } - @Test(expected = PlayerNotFoundException::class) + @Test fun leave_failsWhenPlayerDoesNotExist() { val principal = TestPrincipal("I don't exist") - lobbyController.leave(principal) + + assertFailsWith<PlayerNotFoundException> { + lobbyController.leave(principal) + } } - @Test(expected = PlayerNotInLobbyException::class) + @Test fun leave_failsWhenNotInLobby() { playerRepository.createOrUpdate("testuser", "Test User") val principal = TestPrincipal("testuser") - lobbyController.leave(principal) + + assertFailsWith<PlayerNotInLobbyException> { + lobbyController.leave(principal) + } } @Test @@ -111,7 +118,7 @@ class LobbyControllerTest { assertEquals(reorderedPlayers, lobby.getPlayers()) } - @Test(expected = PlayerIsNotOwnerException::class) + @Test fun reorderPlayers_failsForPeasant() { val player = playerRepository.createOrUpdate("testuser", "Test User") val lobby = lobbyRepository.create("Test Game", player) @@ -124,7 +131,10 @@ class LobbyControllerTest { val reorderPlayersAction = ReorderPlayersAction(playerNames) val principal = TestPrincipal("testuser2") - lobbyController.reorderPlayers(reorderPlayersAction, principal) + + assertFailsWith<PlayerIsNotOwnerException> { + lobbyController.reorderPlayers(reorderPlayersAction, principal) + } } @Test @@ -147,7 +157,7 @@ class LobbyControllerTest { assertEquals(newSettings, lobby.settings) } - @Test(expected = PlayerIsNotOwnerException::class) + @Test fun updateSettings_failsForPeasant() { val player = playerRepository.createOrUpdate("testuser", "Test User") val lobby = lobbyRepository.create("Test Game", player) @@ -158,7 +168,10 @@ class LobbyControllerTest { val updateSettingsAction = UpdateSettingsAction(CustomizableSettings()) val principal = TestPrincipal("testuser2") - lobbyController.updateSettings(updateSettingsAction, principal) + + assertFailsWith<PlayerIsNotOwnerException> { + lobbyController.updateSettings(updateSettingsAction, principal) + } } @Test @@ -176,7 +189,7 @@ class LobbyControllerTest { assertSame(State.PLAYING, lobby.state) } - @Test(expected = PlayerIsNotOwnerException::class) + @Test fun startGame_failsForPeasant() { val player = playerRepository.createOrUpdate("testuser", "Test User") val lobby = lobbyRepository.create("Test Game", player) @@ -185,7 +198,10 @@ class LobbyControllerTest { addPlayer(lobby, "testuser3") val principal = TestPrincipal("testuser2") - lobbyController.startGame(principal) + + assertFailsWith<PlayerIsNotOwnerException> { + lobbyController.startGame(principal) + } } private fun addPlayer(lobby: Lobby, username: String): Player { diff --git a/backend/src/test/kotlin/org/luxons/sevenwonders/lobby/LobbyTest.kt b/backend/src/test/kotlin/org/luxons/sevenwonders/lobby/LobbyTest.kt index 98e154da..967a97e2 100644 --- a/backend/src/test/kotlin/org/luxons/sevenwonders/lobby/LobbyTest.kt +++ b/backend/src/test/kotlin/org/luxons/sevenwonders/lobby/LobbyTest.kt @@ -3,12 +3,10 @@ package org.luxons.sevenwonders.lobby import org.junit.Assume.assumeTrue import org.junit.Before import org.junit.BeforeClass -import org.junit.Rule import org.junit.Test import org.junit.experimental.theories.DataPoints import org.junit.experimental.theories.Theories import org.junit.experimental.theories.Theory -import org.junit.rules.ExpectedException import org.junit.runner.RunWith import org.luxons.sevenwonders.game.api.CustomizableSettings import org.luxons.sevenwonders.game.data.GameDefinition @@ -19,6 +17,7 @@ import org.luxons.sevenwonders.lobby.Lobby.PlayerOverflowException import org.luxons.sevenwonders.lobby.Lobby.PlayerUnderflowException import org.luxons.sevenwonders.lobby.Lobby.UnknownPlayerException import kotlin.test.assertEquals +import kotlin.test.assertFailsWith import kotlin.test.assertFalse import kotlin.test.assertNotNull import kotlin.test.assertSame @@ -27,10 +26,6 @@ import kotlin.test.assertTrue @RunWith(Theories::class) class LobbyTest { - @JvmField - @Rule - var thrown = ExpectedException.none() - private lateinit var gameOwner: Player private lateinit var lobby: Lobby @@ -95,26 +90,32 @@ class LobbyTest { assertSame(lobby, player.lobby) } - @Test(expected = PlayerNameAlreadyUsedException::class) + @Test fun addPlayer_failsOnSameName() { val player = Player("testuser", "Test User") val player2 = Player("testuser2", "Test User") lobby.addPlayer(player) - lobby.addPlayer(player2) + assertFailsWith<PlayerNameAlreadyUsedException> { + lobby.addPlayer(player2) + } } - @Test(expected = PlayerOverflowException::class) + @Test fun addPlayer_playerOverflowWhenTooMany() { - // the owner + the max number gives an overflow - addPlayers(gameDefinition.maxPlayers) + assertFailsWith<PlayerOverflowException> { + // the owner + the max number gives an overflow + addPlayers(gameDefinition.maxPlayers) + } } - @Test(expected = GameAlreadyStartedException::class) + @Test fun addPlayer_failWhenGameStarted() { // total with owner is the minimum addPlayers(gameDefinition.minPlayers - 1) lobby.startGame() - lobby.addPlayer(Player("soonerNextTime", "The Late Guy")) + assertFailsWith<GameAlreadyStartedException> { + lobby.addPlayer(Player("soonerNextTime", "The Late Guy")) + } } private fun addPlayers(nbPlayers: Int) { @@ -124,9 +125,11 @@ class LobbyTest { } } - @Test(expected = UnknownPlayerException::class) + @Test fun removePlayer_failsWhenNotPresent() { - lobby.removePlayer("anyname") + assertFailsWith<UnknownPlayerException> { + lobby.removePlayer("anyname") + } } @Test @@ -157,40 +160,52 @@ class LobbyTest { assertEquals(reorderedUsernames, lobby.getPlayers().map { it.username }) } - @Test(expected = PlayerListMismatchException::class) + @Test fun reorderPlayers_failsOnUnknownPlayer() { val player1 = Player("testuser1", "Test User 1") val player2 = Player("testuser2", "Test User 2") lobby.addPlayer(player1) lobby.addPlayer(player2) - lobby.reorderPlayers(listOf("unknown", "testuser2", "gameowner")) + + assertFailsWith<PlayerListMismatchException> { + lobby.reorderPlayers(listOf("unknown", "testuser2", "gameowner")) + } } - @Test(expected = PlayerListMismatchException::class) + @Test fun reorderPlayers_failsOnExtraPlayer() { val player1 = Player("testuser1", "Test User 1") val player2 = Player("testuser2", "Test User 2") lobby.addPlayer(player1) lobby.addPlayer(player2) - lobby.reorderPlayers(listOf("testuser2", "onemore", "testuser1", "gameowner")) + + assertFailsWith<PlayerListMismatchException> { + lobby.reorderPlayers(listOf("testuser2", "onemore", "testuser1", "gameowner")) + } } - @Test(expected = PlayerListMismatchException::class) + @Test fun reorderPlayers_failsOnMissingPlayer() { val player1 = Player("testuser1", "Test User 1") val player2 = Player("testuser2", "Test User 2") lobby.addPlayer(player1) lobby.addPlayer(player2) - lobby.reorderPlayers(listOf("testuser2", "gameowner")) + + assertFailsWith<PlayerListMismatchException> { + lobby.reorderPlayers(listOf("testuser2", "gameowner")) + } } @Theory fun startGame_failsBelowMinPlayers(nbPlayers: Int) { assumeTrue(nbPlayers < gameDefinition.minPlayers) - thrown.expect(PlayerUnderflowException::class.java) + // there is already the owner addPlayers(nbPlayers - 1) - lobby.startGame() + + assertFailsWith<PlayerUnderflowException> { + lobby.startGame() + } } @Theory diff --git a/backend/src/test/kotlin/org/luxons/sevenwonders/repositories/LobbyRepositoryTest.kt b/backend/src/test/kotlin/org/luxons/sevenwonders/repositories/LobbyRepositoryTest.kt index 84f7eacf..446feee6 100644 --- a/backend/src/test/kotlin/org/luxons/sevenwonders/repositories/LobbyRepositoryTest.kt +++ b/backend/src/test/kotlin/org/luxons/sevenwonders/repositories/LobbyRepositoryTest.kt @@ -3,6 +3,7 @@ package org.luxons.sevenwonders.repositories import org.junit.Before import org.junit.Test import org.luxons.sevenwonders.lobby.Player +import kotlin.test.assertFailsWith import kotlin.test.assertNotNull import kotlin.test.assertSame import kotlin.test.assertTrue @@ -38,9 +39,11 @@ class LobbyRepositoryTest { assertTrue(lobby.isOwner(owner.username)) } - @Test(expected = LobbyNotFoundException::class) + @Test fun find_failsOnUnknownId() { - repository.find(123) + assertFailsWith<LobbyNotFoundException> { + repository.find(123) + } } @Test @@ -52,9 +55,11 @@ class LobbyRepositoryTest { assertSame(lobby2, repository.find(lobby2.id)) } - @Test(expected = LobbyNotFoundException::class) + @Test fun remove_failsOnUnknownId() { - repository.remove(123) + assertFailsWith<LobbyNotFoundException> { + repository.remove(123) + } } @Test diff --git a/backend/src/test/kotlin/org/luxons/sevenwonders/repositories/PlayerRepositoryTest.kt b/backend/src/test/kotlin/org/luxons/sevenwonders/repositories/PlayerRepositoryTest.kt index e364275d..aeedc54c 100644 --- a/backend/src/test/kotlin/org/luxons/sevenwonders/repositories/PlayerRepositoryTest.kt +++ b/backend/src/test/kotlin/org/luxons/sevenwonders/repositories/PlayerRepositoryTest.kt @@ -3,6 +3,7 @@ package org.luxons.sevenwonders.repositories import org.junit.Before import org.junit.Test import kotlin.test.assertEquals +import kotlin.test.assertFailsWith import kotlin.test.assertFalse import kotlin.test.assertSame import kotlin.test.assertTrue @@ -42,9 +43,11 @@ class PlayerRepositoryTest { assertEquals("Much Better Name", player1Updated.displayName) } - @Test(expected = PlayerNotFoundException::class) + @Test fun find_failsOnUnknownUsername() { - repository.find("anyUsername") + assertFailsWith<PlayerNotFoundException> { + repository.find("anyUsername") + } } @Test @@ -55,9 +58,11 @@ class PlayerRepositoryTest { assertSame(player2, repository.find("player2")) } - @Test(expected = PlayerNotFoundException::class) + @Test fun remove_failsOnUnknownUsername() { - repository.remove("anyUsername") + assertFailsWith<PlayerNotFoundException> { + repository.remove("anyUsername") + } } @Test diff --git a/backend/src/test/kotlin/org/luxons/sevenwonders/validation/DestinationAccessValidatorTest.kt b/backend/src/test/kotlin/org/luxons/sevenwonders/validation/DestinationAccessValidatorTest.kt index 88853b6c..85d03e99 100644 --- a/backend/src/test/kotlin/org/luxons/sevenwonders/validation/DestinationAccessValidatorTest.kt +++ b/backend/src/test/kotlin/org/luxons/sevenwonders/validation/DestinationAccessValidatorTest.kt @@ -6,6 +6,7 @@ import org.luxons.sevenwonders.lobby.Lobby import org.luxons.sevenwonders.lobby.Player import org.luxons.sevenwonders.repositories.LobbyNotFoundException import org.luxons.sevenwonders.repositories.LobbyRepository +import kotlin.test.assertFailsWith import kotlin.test.assertFalse import kotlin.test.assertTrue @@ -68,28 +69,36 @@ class DestinationAccessValidatorTest { assertTrue(destinationAccessValidator.hasAccess("testUser", "/lobby/notANumber/suffix")) } - @Test(expected = LobbyNotFoundException::class) + @Test fun validate_failWhenNoLobbyExist() { - destinationAccessValidator.hasAccess("", "/lobby/0") + assertFailsWith<LobbyNotFoundException> { + destinationAccessValidator.hasAccess("", "/lobby/0") + } } - @Test(expected = LobbyNotFoundException::class) + @Test fun validate_failWhenNoGameExist() { - destinationAccessValidator.hasAccess("", "/game/0") + assertFailsWith<LobbyNotFoundException> { + destinationAccessValidator.hasAccess("", "/game/0") + } } - @Test(expected = LobbyNotFoundException::class) + @Test fun validate_failWhenReferencedLobbyDoesNotExist() { createLobby("Test Game", "ownerUser1") createLobby("Test Game 2", "ownerUser2") - destinationAccessValidator.hasAccess("doesNotMatter", "/lobby/3") + assertFailsWith<LobbyNotFoundException> { + destinationAccessValidator.hasAccess("doesNotMatter", "/lobby/3") + } } - @Test(expected = LobbyNotFoundException::class) + @Test fun validate_failWhenReferencedGameDoesNotExist() { createGame("Test Game 1", "user1", "user2", "user3") createGame("Test Game 2", "user4", "user5", "user6") - destinationAccessValidator.hasAccess("doesNotMatter", "/game/3") + assertFailsWith<LobbyNotFoundException> { + destinationAccessValidator.hasAccess("doesNotMatter", "/game/3") + } } @Test |