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 a flaky test #39

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

Conversation

Carol7102
Copy link

@Carol7102 Carol7102 commented Nov 27, 2023

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 ApplicationContextTest class located at tiny-spring/src/test/java/us/codecraft/tinyioc/context/ApplicationContextTest.java.

Root cause

The root cause of the flakiness was related to access a field using getDeclaredField() with the name provided in propertyValue.getName(), but this field doesn't exist in the class of the bean working with. This results in a NoSuchFieldException.

Fix

This test has been resolved by handling NoSuchFieldException to guarantee there must be a valid field before accessing it using getDeclaredField().

This fix is significant because it eliminates the uncertain field names introduced by different beans, 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
if not set Java Home path:
add following lines into /etc/profile
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
export PATH=$PATH:$JAVA_HOME/bin
and run
source /etc/profile

  1. Compile the module
    mvn install -pl . -am -DskipTests
  2. Run regular tests
    mvn -pl . test -Dtest=us.codecraft.tinyioc.context.ApplicationContextTest#testPostBeanProcessor
  3. Run tests with NonDex tool
    mvn -pl . edu.illinois:nondex-maven-plugin:2.1.1:nondex -Dtest=us.codecraft.tinyioc.context.ApplicationContextTest#testPostBeanProcessor
    After fix, all tests pass

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