TerrainServiceClass ‐ Unit testing - UQcsse3200/2023-studio-2 GitHub Wiki

TerrainServiceTest Class Documentation

Overview

The TerrainServiceTest class is a JUnit test class designed to test the functionality of the TerrainService class in your game. The TerrainService class is responsible for converting screen coordinates to game coordinates using the TerrainComponent class. This test class uses Mockito to mock the behavior of the TerrainComponent and verify that the conversion is performed correctly.

Table of Contents

  1. Prerequisites
  2. Test Description
  3. Test Setup
  4. Test Execution
  5. Expected Results
  6. Conclusion

Prerequisites

Before running the TerrainServiceTest class, ensure that you have the following prerequisites in place:

  • A working Java development environment.
  • JUnit and Mockito dependencies added to your project.
  • The TerrainComponent class that is being tested.

Test Description

The primary objective of the TerrainServiceTest class is to verify that the ScreenCoordsToGameCoords method of the TerrainService class correctly converts screen coordinates (pixel positions on the screen) to game coordinates (in-game positions). This test is essential to ensure that the game accurately maps user input to the game world.

UML

Mocking TerrainComponent

In this test, the TerrainComponent is mocked using Mockito. The following steps are involved in setting up the mock:

  1. Import relevant classes and dependencies.
  2. Create a mock instance of TerrainComponent using mock(TerrainComponent.class) in the setUp method.

Test Execution

Conversion Testing

The testScreenCoordsToGameCoords method is responsible for testing the ScreenCoordsToGameCoords method of the TerrainService class. Here's what happens in this method:

  1. An instance of TerrainService is created, passing the mocked TerrainComponent as a parameter.
  2. Screen coordinates (screenX and screenY) are defined.
  3. The behavior of the TerrainComponent's unproject method is mocked using Mockito's when method to return a predefined Vector3 result when called with the provided screen coordinates.
  4. The ScreenCoordsToGameCoords method is called with the screen coordinates.
  5. The result is compared with the expected Vector2 result.

Expected Results

The expected result of running the TerrainServiceTest class is that the ScreenCoordsToGameCoords method of the TerrainService class correctly converts screen coordinates to game coordinates. The assertEquals method is used to compare the actual result with the expected result. If the test passes, it indicates that the conversion logic is working as intended.

Conclusion

The TerrainServiceTest class is an essential part of your game development process, ensuring that the TerrainService accurately translates user input into game world coordinates. By mocking the TerrainComponent and setting up test cases, you can verify the correctness of this critical functionality, contributing to a more reliable and robust game.