Skip to content

GameMap Test Instances

HunterWhitty edited this page Oct 14, 2023 · 41 revisions

Some methods have been introduced to help create test GameMap instances for use in JUnit testing (to get that sweet code coverage). An additional method has also been included to load an alternate map into the SpaceGameArea class during player testing. The methods introduced for JUnit testing do not create a TerrainComponent to render the terrain since that would only add unnecessary complications. An example of how to create a test instance for JUnit testing will be shown below.

Creating your own maps for testing

Test maps are stored in the assets/configs/TestMaps/. When using test maps stored in this location, file paths should read configs/TestMaps/[map name].txt when being passed to methods. A series of test maps have been added for you to use in JUnit testing:

  • allDirt20x20_map.txt: a 20x20 map of just dirt tiles (movement modifier on dirt TerrainTiles is 1.0).
  • allTraversable20x20_map.txt: a 20x20 map of traversable tiles.
  • DirtWithWaterBorder20x20_map.txt: a 20x20 map of dirt tiles surrounded by DeepWater tiles that are not traversable.
  • exampleTestMap.txt: a 120x120 map of dirt tiles.

You can create your own map files for JUnit or playtesting, either through manually creating a textile and matching characters to their TerrainTile equivalents or by using a Tiled level editor program (more can be found about TerrainTiles and map loading here). When creating our initial map, we used this tile editor. You can load tile images into the editor and assign them corresponding characters. The map can then be exported as a CSV file, which will have to be converted into a txt file for the map loading function to be able to read the provided file. You will also have to provide the x and y dimensions at the top of the txt map file. If creating any new maps, make sure to add them to the assets/configs/TestMaps/ folder and to follow the current naming convention.

Loading a different map into the SpaceGameArea

Loading in a map from the assets/configs/TestMaps/ folder can be done using the createTestTerrainComponent method. The file path is the only argument for this method. You should comment out the createTerrainComponent method in the spawnTerrain method in the SpaceGameArea class and replace it with the createTestTerrainComponent method.

image

The below screenshot shows how the game has loaded with a different map layout.

image

It is important to note that creating maps that are too small while play testing may crash the game. This is because teams have designed components to fit within the original 100x100 tiled map, meaning smaller maps may have some oddities occurring.

JUnit testing with the GameMap class

The loadTestTerrain method was added to the GameMap class to facilitate JUnit testing when the components necessary for TerrainComponent creation were not available. The method goes through the same processes as the createTestTerrainComponent method but does not create a TerrainComponent.

  • An example of how a GameMap test instance is created for JUnit tests where code is interacting with the map is in the com/csse3200/game/components/player/PlayerMovementIntegrationTest.java class.
  • An example of how a GameMap test instance is created for JUnit tests where a mock GameMap instance is needed is in the com/csse3200/game/components/player/InventoryHotkeyTest.java class.

Important parts to implement to include test GameMap instances in JUnit tests:

  • In the @BeforeAll method, you need to create the test GameMap instance. It can either be mocked like in the InventoryHotkeyTest.java class or instantiated with TerrainTiles like in the PlayerMovementIntegrationTest.java class. This GameMap test instance is assigned to the TestGameArea instance.
  • In the @BeforeEach method, you need to register the TestGameArea instance with the ServiceLocator.
  • In the @AfterEach method, you need to clear the ServiceLocator.
Clone this wiki locally