Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix flaky test br.com.helpdev.quaklog.entity.GameTest#shouldBuildGameWithSuccess #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

hofi1
Copy link

@hofi1 hofi1 commented Oct 31, 2023

Problem

The test test br.com.helpdev.quaklog.entity.GameTest#shouldBuildGameWithSuccess asserts the players which are stored in the game. These are stored in a List. The list is generated by the builder using a map. But the order, in which the players are returned by the map, is not deterministic, but the test expects the values to be in certain order.
This leads to a flaky test.

br.com.helpdev.quaklog.entity.GameTest > shouldBuildGameWithSuccess() FAILED
    org.opentest4j.AssertionFailedError: expected: <Mock for PlayerInGame, hashCode: 461659368> but was: <Mock for PlayerInGame, hashCode: 1532150109>
        at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:56)
        at org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:197)
        at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:186)
        at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:181)
        at org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:500)
        at br.com.helpdev.quaklog.entity.GameTest.shouldBuildGameWithSuccess(GameTest.java:39)

final var game = gameBuilder.build();
assertEquals(playerInGame, game.getPlayers().get(0));

this.players = List.copyOf(builder.players.values());

This problem was found by the NonDex Engine.

Solution

Sort the stream of players in the map by their ID, to make sure, they are returned in predefined order.

this.players = List.copyOf(builder.players.values().stream().sorted(
Comparator.comparing(PlayerInGame::getId)).collect(Collectors.toList()));

Reproduce

To reproduce, follow the steps:

  1. ./gradlew build -x test
  2. Add the following text to the top of the build.gradle file in $PROJ_DIR.
buildscript {
    repositories {
      maven {
        url = uri('https://plugins.gradle.org/m2/')
      }
    }
    dependencies {
      classpath('edu.illinois:plugin:2.1.1')
    }
}
  1. Add the following line to the end of the build.gradle file in $PROJ_DIR.
apply plugin: 'edu.illinois.nondex'
  1. Add the following line to the end of the build.gradle file in $PROJ_DIR.
subprojects {
  apply plugin: 'edu.illinois.nondex'
}
  1. Change the following lines
test {
  useJUnitPlatform()
}
// Change above to below
tasks.withType(Test) {
  useJUnitPlatform()
}
  1. Run
./gradlew --info nondexTest --tests=br.com.helpdev.quaklog.entity.GameTest.shouldBuildGameWithSuccess --nondexRuns=100 -p core

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant