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
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
TerrainComponent
Mocking In this test, the TerrainComponent
is mocked using Mockito. The following steps are involved in setting up the mock:
- Import relevant classes and dependencies.
- Create a mock instance of
TerrainComponent
usingmock(TerrainComponent.class)
in thesetUp
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:
- An instance of
TerrainService
is created, passing the mockedTerrainComponent
as a parameter. - Screen coordinates (
screenX
andscreenY
) are defined. - The behavior of the
TerrainComponent
'sunproject
method is mocked using Mockito'swhen
method to return a predefinedVector3
result when called with the provided screen coordinates. - The
ScreenCoordsToGameCoords
method is called with the screen coordinates. - 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.