This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Flaky tests are common occurrences in open-source projects, yielding inconsistent results—sometimes passing and sometimes failing—without code changes. NonDex is a tool for detecting and debugging wrong assumptions on under-determined Java APIs. I have resolved a flaky test issue using NonDex tool, specifically in the CountryServiceTest class located at
restcountries/src/main/java/eu/fayder/restcountries/v2/rest/CountryService.java
.Root cause
The root cause of the flakiness was related to access an element in the countries List that is null while executing the getByRegionalBloc method in your test case. The test is failing because of a java.lang.NullPointerException that occurs when the code is unable to handle null properly. As a result, country.getRegionalBlocs() return a NULL list, it causes intermittent failures.
Fix
This test has been resolved by checking for null or empty lists before iterating through them to ensure that countries is initialized and populated before attempting to iterate through it in the getByRegionalBloc method.
This fix is significant because it eliminates the uncertainty introduced by different output from country.getRegionalBlocs(), ensuring that the test consistently passes across different test runs. By doing so, we have improved the reliability and stability of our testing suite.
How to test
Java: openjdk version "11.0.20.1"
Maven: Apache Maven 3.6.3
mvn install -pl . -am -DskipTests
mvn -pl . test -Dtest=eu.fayder.restcountries.v2.CountryServiceTest#getByRegionalBloc
mvn -pl . edu.illinois:nondex-maven-plugin:2.1.1:nondex -Dtest=eu.fayder.restcountries.v2.CountryServiceTest#getByRegionalBloc
After fix, all tests pass