protegeproject / webprotege

The webprotege code base
Other
628 stars 253 forks source link

Fix Flaky Test in SubjectClosureResolver_TestCase #814

Open Sitara-a opened 2 weeks ago

Sitara-a commented 2 weeks ago

A flaky test was fixed in this pull request-

  1. edu.stanford.bmir.protege.web.server.entity.SubjectClosureResolver_TestCase.shouldGetClsAsRootEntity

Flakiness in the tests -

To check for flakiness in the tests, a tool called nondex was used.

Steps to reproduce flakiness using nondex -

Test 1:

mvn install -pl webprotege-server-api -am -DskipTests
mvn -pl webprotege-server-api test -Dtest=edu.stanford.bmir.protege.web.server.entity.SubjectClosureResolver_TestCase
mvn -pl webprotege-server-api edu.illinois:nondex-maven-plugin:2.1.7:nondex -Dtest=edu.stanford.bmir.protege.web.server.entity.SubjectClosureResolver_TestCase

The third command shows us the flakiness in the test. There are test failures due to differences in the order of elements

Screenshot 2024-10-31 140226

Source of Flakiness in the test and fix: The object rootEntities is a set and sets in Java have no order. However, the check being carried out was - assertThat(rootEntities, contains(cls, valueEntity)); The contains() method checks if the two objects cls and valueEntity are present in the exact order in which they are mentioned and this fails if the order in the set is different from the order mentioned.

To fix this, contains() is replaced by hasItems() which does not check the order of items.

Code change- assertThat(rootEntities, hasItems(cls, valueEntity));

Please let me know if you have any questions or need any additional justification/changes from my side.