Achievements System Testing - UQdeco2800/2021-ext-studio-2 GitHub Wiki

Testing Ecosystem

Note: Sprint 4 updates included.

The GameExtension class makes the job of testing very easy by mocking most of the libgdx and game-engine classes.

@ExtendWith(GameExtension.class)
public class AchievementStatsComponentTest {
  • Java Lang Reflect: Used to access private methods and private fields of a class from the testing suite and make them accessible for testing purposes. It finds its applications when private static, private, and private static final variables and methods need to be altered for ease of testing.

  • JUnit: Used for assertions (mostly value equality) and class extendWitih decorator plugins.

  • Mockito: Mocking static classes and simulating return values of static methods in order to control certain variables while performing the test in order to reduce complexity.

Team 7 Test Plan - Sprint 1

Test achievement unlocking logic, i.e, the logic which decides that an achievement popup should be rendered on the screen.

The most sensitive logic of the achievements system is to check if - given the in game time, item count and health, a certain achievement is valid or not. An event must be dispatched if the achievement is valid given the time, item count and health constraints. An event called updateAchievement can only be dispatched if the particular achievement satisfies the constraints, in which case it is unlocked and perfectly valid.

Accessing private methods

private Method getIsValidMethod() throws NoSuchMethodException {
        Method method = AchievementsStatsComponent.class
                .getDeclaredMethod("isValid",
                        BaseAchievementConfig.class);
        method.setAccessible(true);
        return method;
}

Sample test where the Veteran achievement should be valid

The veteran achievement gets unlocked if the user survives for 10, 15 or 20 minutes. It is assumed that the JSON achievement details configuration is valid.

@Test
void shouldCorrectlyValidateVeteranAchievements() throws
            IllegalAccessException,
            InvocationTargetException, NoSuchMethodException {

        Method method = getIsValidMethod();

        AchievementsStatsComponent component = generateEntity()
                .getComponent(AchievementsStatsComponent.class);

        for (BaseAchievementConfig a : getList()) {
            component.setTime(999999999);
            component.setHealth(-1);

            if (a.name.equals("Veteran")) {
                assertEquals(method.invoke(component, a), true);
            } else {
                assertEquals(method.invoke(component, a), false);
            }
        }
    }

Achievements already tested

  • Veteran
  • Game Breaker
  • Tool Master

Team 7 Test Plan - Sprint 2

Test GameRecords and GameInfo persistence logic. More information about this flow can be found here

GameInfo

Accessing private static final variables to safely test files/GameInfo.java!

This setup was necessary in order to make sure that the test suite's FileLoader does not communicate with the filesystem outside the project folder. So all the JSON operations were redirected to test files inside source/assets/test/files directory and thats where fully fledged test read and write operations took place. This was necessary to test the GameInfo which kept a tab of the number of games played. The incremement logic was tested as well which happens as soon as a player dies.

    /**
     * Change the value of a private static final variable
     *
     * @param field    the private static final variable field
     * @param newValue the value to replace
     */
    static void setFinalStatic(Field field, Object newValue) {
        try {
            field.setAccessible(true);

            Field modifiersField = Field.class.getDeclaredField("modifiers");
            modifiersField.setAccessible(true);
            modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

            field.set(null, newValue);
        } catch (Exception ignored) {

        }
    }


    @BeforeEach
    void beforeEach() throws Exception {
        // Changing the JSON file path so that the original game data in the EXTERNAL location is not affected
        setFinalStatic(GameInfo.class.getDeclaredField("path"),
                "test/files/gameInfoTest.json");
        setFinalStatic(GameInfo.class.getDeclaredField("location"), FileLoader.Location.LOCAL);
    }

For reference, these are the actual variables in the GameInfo.java's class:

    private static final String path = ROOT_DIR + File.separator + GAME_INFO_FILE;
    private static final FileLoader.Location location = FileLoader.Location.EXTERNAL;

Unlockable Chapters

Mocking static classes

    MockedStatic<GameRecords> gameInfoMockedStatic =
            Mockito.mockStatic(GameRecords.class);

The game needs to be played in order to unlock gold achievements. Gold achievements are needed to unlock chapters. Since this is not feasible, the GameRecords class needs to be mocked in order to return unrealistic records, suitable for testing.

    private void setGoldAchievementsToBeReturned(int count) {
        gameInfoMockedStatic.when(GameRecords::getGoldAchievementsCount)
                .thenReturn(count);
        assertEquals(GameRecords.getGoldAchievementsCount(), count);
    }

    @AfterEach
    void afterEach() {
        // IMPORTANT! Each static mock can only be registered once. 
        // So it needs to be deregistered after every test run
        gameInfoMockedStatic.close();
    }

More achievements tested

Achievements tested in Sprint 2

  • Master
  • Healer
  • Stranger

The tests can be found in AchievementStatsComponentTest.java.

This is what a typical test looks like for the Stranger achievement which involves surviving for a particular amount of time without picking up any items.

    @Test
    void shouldCorrectlyValidateStrangerAchievements() throws
            IllegalAccessException,
            InvocationTargetException, NoSuchMethodException {

        Method method = getIsValidMethod();


        AchievementsStatsComponent component = generateEntity()
                .getComponent(AchievementsStatsComponent.class);

        component.setTime(999999999);
        component.setItemCountByVal(0);
        BaseAchievementConfig achievement = genAchievement("Stranger", 10, -1, 0, -1, -1);
        assertEquals(method.invoke(component, achievement), true);
        achievement.unlocked = false;
        component.setTime(999999999);
        component.setItemCountByVal(0);
        achievement = genAchievement("Stranger", 15, -1, 0, -1, -1);
        assertEquals(method.invoke(component, achievement), true);
        achievement.unlocked = false;
        component.setTime(999999999);
        component.setItemCountByVal(0);
        achievement = genAchievement("Stranger", 20, -1, 0, -1, -1);
        assertEquals(method.invoke(component, achievement), true);
        achievement.unlocked = false;

        component.setItemCountByVal(-1);
        assertEquals(method.invoke(component, achievement), false);

Master achievements are simpler and involve crossing certain values of the in game score.

    @Test
    void shouldCorrectlyValidateMasterAchievements() throws
            NoSuchMethodException,
            InvocationTargetException, IllegalAccessException {

        Method method = getIsValidMethod();

        AchievementsStatsComponent component = generateEntity()
                .getComponent(AchievementsStatsComponent.class);
        for (BaseAchievementConfig a : getList()) {
            component.setScore(999999999);

            if (a.name.equals("Master")) {
                assertEquals(method.invoke(component, a), true);
            } else {
                assertEquals(method.invoke(component, a), false);
            }
        }
    } 

Team 7 Test Plan - Sprint 3

Achievements tested in this sprint

  • Alphamineron (Boss achievement)
  • Survival Expert
  • Collector
  • Game Fanatic

The boss achievement was tested in this Sprint. The name of this achievement is Alphamineron and this achievement gets triggered when the main character has successfully avoided the boss spaceship. The idea is to simulate the condition where the success flag of avoiding the spaceship is set to true, and then check if the achievement has unlocked or not.

    /**
     * Tests if Alphamineron achievement are un-lockable
     *
     * @throws NoSuchMethodException     thrown if corresponding method doesn't exist
     * @throws InvocationTargetException wraps exception thrown by constructor invoked
     * @throws IllegalAccessException    thrown if access is not allowed for a method
     */
    @Test
    void shouldCorrectlyValidateAlphamineronAchievement() throws
            IllegalAccessException,
            InvocationTargetException, NoSuchMethodException {

        Method method = getIsValidMethod();

        // Get component from mocked entity
        AchievementsStatsComponent component = generateEntity()
                .getComponent(AchievementsStatsComponent.class);

        for (BaseAchievementConfig a : getList()) {
            component.setSpaceshipAvoidSuccess();
            // Only the Alphamineron achievement should be valid if
            // the spaceship has been avoided
            if (a.name.equals("Alphamineron")) {
                assertEquals(method.invoke(component, a), true);
            } else {
                // Other achievements should be invalid in this condition
                assertEquals(method.invoke(component, a), false);
            }
        }
    }

Another achievement of note is the Game Fanatic achievement. This achievement gets unlocked when the user picks up a particular number of first aids while having full health. The health and first aid pick ups are then simulated to check the correctness of the Game Fanatic achievement logic.

        // Invalid number of first aid count values
        component.setFirstAidByVal(-1);
        assertEquals(method.invoke(component, achievement), false);
        component.setFirstAidByVal(-3);
        assertEquals(method.invoke(component, achievement), false);
        component.setFirstAidByVal(-99);
        assertEquals(method.invoke(component, achievement), false);

        // Simulate 3 first aids picked up, which is a valid.
        component.setFirstAidByVal(3);
        // When the health is boosted, the achievement should still be valid
        component.setHealth(110);
        assertEquals(method.invoke(component, achievement), true);
        // Lock the achievement again
        achievement.unlocked = false;
        // The health value is high, but not full. The achievement should be invalid.
        component.setHealth(99);
        assertEquals(method.invoke(component, achievement), false);
        // Moderate health, achievement should not be unlocked
        component.setHealth(40);
        assertEquals(method.invoke(component, achievement), false);
        // Locked in low health conditions
        component.setHealth(0);
        assertEquals(method.invoke(component, achievement), false);
        component.setHealth(1);
        assertEquals(method.invoke(component, achievement), false);
        // Locked when the health value is invalid
        component.setHealth(-1);
        assertEquals(method.invoke(component, achievement), false);

Team 7 Test Plan - Sprint 4

In this sprint,GameRecordUtils utility methods were tested. The file can be found here.

Tested:

  • Testing getHighestScores utility method which fetches score records from persisted gameRecords.json and returns the list of score objects
  • Testing getBestAchievementsByGame utility method which fetches the highest tier achievements of a particular game from persisted gameRecords.json

The general flow involved mocking the GameRecords methods, API reference for which can be found here.

MockedStatic<GameRecords> gameRecordsMockedStatic =
            Mockito.mockStatic(GameRecords.class);
// Mocking method which interfaces with FileLoader (which does not work in test suite)
// Mock the static method and return the generated list
gameRecordsMockedStatic.when(() -> GameRecords.getAchievementsByGame(Mockito.anyInt()))
                .thenReturn(achievementList);
// Mock the static method and return the generated list
gameRecordsMockedStatic.when(GameRecords::getAllScores).thenReturn(scoreList);

Notice how parameterized static methods are mocked:

// Mocking method which interfaces with FileLoader (which does not work in test suite)
// Mock the static method and return the generated list
gameRecordsMockedStatic.when(() -> GameRecords.getAchievementsByGame(Mockito.anyInt()))
                .thenReturn(achievementList);

After mocking setup, logic is tested:

List<BaseAchievementConfig> bestAchievements = GameRecordUtils.getBestAchievementsByGame(99);
// Based on mocked data, should return only 5 achievements (only the highest tier ones)
assertEquals(bestAchievements.size(), 5);
// Setup mocks
mockScoreList();

List<GameRecords.Score> highestScores = GameRecordUtils.getHighestScores();

int[] highestScoresExpected = {99, 51, 23, 23, 1};

for (int i = 0; i < highestScoresExpected.length; i++) {
   assertEquals(highestScoresExpected[i], highestScores.get(i).score);
}

After each test case, close down the mocked instance to reuse it:

@AfterEach
void afterEach() {
    gameRecordsMockedStatic.close();
}

Future testing plan (Team 7)

  • Testing the asynchronous event queue (AsyncTaskQueue.java) which is used to display each popup for a fixed time interval
  • Refactoring AchievementsStatsComponentTest file to look cleaner, which will also involve making the logic of AchievementStatsComponent more modular and concise, and remove a lot of duplicate patterns.
  • UI Testing of AchievementsDisplay.java, ScoreDetailsDialog.java,ChapterDisplay.java and BackgroundSelectionComponent.java
  • Testing BackgroundMusic.java using PowerMockito in order to mock static method invocations within other static methods
  • Testing GameRecords.java using PowerMockito in order to mock static method invocations within other static methods