Projectile Test - UQcsse3200/2024-studio-1 GitHub Wiki
ProjectileAttackComponentTest
ProjectileAttackComponentTest
is a unit test class designed to validate the behavior of the ProjectileAttackComponent
. It tests the attack functionality, including collision detection and damage application, ensuring that projectiles behave correctly during interactions.
Class Description
The ProjectileAttackComponentTest
class uses JUnit 5 framework and integrates with game services to set up testing environments. It verifies that projectiles deal damage correctly upon hitting targets and checks interactions across various physics layers.
Key Methods:
-
@BeforeEach void beforeEach()
- Description: Sets up the necessary game services (like Physics, Resource, Render, and Entity Services) for tests. It loads the projectile texture from the current configuration, ensuring textures are ready for testing.
-
@Test void shouldHit()
- Description: Tests whether a projectile correctly damages a target upon collision. It asserts that the target's health is zero after the collision.
-
@Test void shouldNotAttackOtherLayer()
- Description: Validates that projectiles do not damage entities on layers other than their designated target layer. It asserts that the health of the target remains unchanged when it is on a different layer.
-
@Test void shouldNotAttackWithoutCombatComponent()
- Description: Tests that a projectile does not cause damage if the target entity does not have a
CombatStatsComponent
. It ensures that no exceptions are thrown during the collision event.
- Description: Tests that a projectile does not cause damage if the target entity does not have a
Helper Methods:
-
Entity createProjectile()
- Description: Creates and returns a new projectile entity using the
ProjectileFactory
with a default projectile configuration and direction.
- Description: Creates and returns a new projectile entity using the
-
Entity createTarget()
- Description: Creates and returns a new target entity configured with health and a physics component, designed to interact with the projectile.
ProjectileActionsTest
ProjectileActionsTest
is a unit test class focused on validating the behavior of the ProjectileActions
component. The tests ensure that projectiles move correctly in specified directions and at expected speeds during the game.
Class Description
The ProjectileActionsTest
class uses JUnit 5 and Mockito to create a controlled environment for testing projectile movement. It checks whether projectiles move as intended based on given directions and speeds. The tests utilize mocked services to isolate projectile behavior from the entire game engine while verifying interactions.
Key Methods:
-
@BeforeEach void beforeEach()
- Description: Initializes services necessary for testing, including rendering, physics, and resource services. It also loads required textures into the resource service, ensuring that all assets are ready for testing.
-
@org.junit.jupiter.api.Test void shouldMove()
- Description: Tests if the projectile moves in the expected direction when shot. It asserts that the distance moved is greater than zero after updating the projectile for several cycles.
-
@org.junit.jupiter.api.Test void shouldMoveLeft()
- Description: Validates that a projectile moves left when shot in that direction. It checks that the y-coordinate remains the same while the x-coordinate decreases.
-
@org.junit.jupiter.api.Test void shouldMoveRight()
- Description: Confirms that a projectile moves right when the direction is set accordingly. It asserts that the y-coordinate stays the same while the x-coordinate increases.
-
@org.junit.jupiter.api.Test void shouldMoveUp()
- Description: Ensures that a projectile shot upwards moves in the correct direction by checking that the x-coordinate remains constant while the y-coordinate increases.
-
@org.junit.jupiter.api.Test void shouldMoveDown()
- Description: Checks that a projectile moves downwards when shot in that direction, asserting that the y-coordinate decreases while the x-coordinate stays the same.
-
@org.junit.jupiter.api.Test void shouldMoveMultiple()
- Description: Tests that multiple projectiles moving in different directions do so independently and correctly. It asserts that both projectiles have moved from their initial positions.
-
@org.junit.jupiter.api.Test void shouldMoveAtSpeed()
- Description: Validates that projectiles with different speeds (fast and slow) move accordingly when they are shot. It asserts that the distance moved by the slower projectile is less than that moved by the faster one.
Helper Methods:
- Entity createProjectile(Vector2 direction)
- Description: A utility method that creates and returns a new projectile entity using the
ProjectileFactory
with the specified direction. The projectile is configured and initialized before being returned.
- Description: A utility method that creates and returns a new projectile entity using the
ProjectileAnimationControllerTest
The ProjectileAnimationControllerTest class is a unit test designed to validate the behavior of the ProjectileAnimationController. It ensures that the animation controller can be created and operates without throwing exceptions, laying the groundwork for more comprehensive animation tests in future scenarios.
Class Description
The ProjectileAnimationControllerTest class utilizes the JUnit 5 framework, integrating with the game's service architecture to establish a controlled testing environment. It verifies that the animation controller can be instantiated and that the create() method functions as expected without exceptions.
Key Methods:
-
@BeforeEach void beforeEach()
- Description: Sets up the necessary game services, including Resource, Physics, and Entity Services, prior to executing each test. It registers these services with the ServiceLocator, ensuring that the testing environment is correctly configured. A projectile entity is created using the ProjectileFactory, and the associated ProjectileAnimationController is retrieved for testing.
-
@Test void testCreate()
- Description: Tests the create() method of the ProjectileAnimationController. This test verifies that the method executes without throwing any exceptions, ensuring that the controller can initialize properly.
Helper Methods:
-Entity createProjectile()
- Description: Creates and returns a new projectile entity using the ProjectileFactory with a predefined configuration and initial position. This method simplifies the creation of projectile instances for testing.
-ProjectileAnimationController getAnimationController(Entity projectile)
- Description: Retrieves and returns the ProjectileAnimationController component from the specified projectile entity. This helper method provides a streamlined way to access the animation controller for testing purposes.