Skip to content

GameMap Test Instances

HunterWhitty edited this page Oct 3, 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

Store test maps in assets/configs/TestMaps/, file paths should read configs/TestMaps/[map name].txt when being passed to methods <--- need to improve.

Loading in 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 test GameMap instance is created and used can be found in the test/com/csse3200/game/areas/terrain/GameMapTest.java file.

When creating GameMap variables to be used in JUnit tests, they should be initialised as static final variables. This is so they can be modified in the static @BeforeAll JUnit method. Additionally, A ResourceService variable should be initialised as a static final variable to allow the loading of assets necessary to create the map (dimensions are needed from the assets).


private static final GameMap gameMap = new GameMap(new TerrainFactory(new CameraComponent()));

private static final ResourceService resourceService = new ResourceService();


In the @BeforeAll JUnit method (the config method in the GameMapTest class), the ResourceService instance should be loaded with assets and the GameMap instance should be used to load a test map using the loadTestTerrain method. A mock TerrainComponent instance will need to be instantiated for the loadTestTerrain method.


resourceService.loadTextures(TerrainFactory.mapTextures);

resourceService.loadAll();

ServiceLocator.registerResourceService(resourceService);

TerrainComponent terrainComponent = mock(TerrainComponent.class);

doReturn(TerrainFactory.worldTileSize).when(terrainComponent).getTileSize();

gameMap.setTerrainComponent(terrainComponent);

gameMap.loadTestTerrain("configs/TestMaps/gameMapTest_map.txt");


Clone this wiki locally