Fixed non-idempotent unit tests in `JSONObjectTest` by kaiyaok2 · Pull Request #889 · stleary/JSON-java · GitHub
Skip to content

Fixed non-idempotent unit tests in JSONObjectTest#889

Merged
stleary merged 1 commit into
stleary:masterfrom
kaiyaok2:fix_nio_tests
May 3, 2024
Merged

Fixed non-idempotent unit tests in JSONObjectTest#889
stleary merged 1 commit into
stleary:masterfrom
kaiyaok2:fix_nio_tests

Conversation

@kaiyaok2

Copy link
Copy Markdown
Contributor

Motivation:

A couple of tests in JSONObjectTest are not idempotent and fail in the second runs in the same JVM, because they pollute states reused by themselves. Specifically, these tests first assume that SingletonEnum.getInstance() or Singleton.getInstance() are in a clean state, and then modify them. However, these instances are not cleaned up after the tests, so the repeated runs fail in the initial sanity check. It shall be good to clean the state pollution so that potential newly introduced tests do not fail in the future due to the shared state polluted by these tests.

Two non-idempotent tests are:

  • testSingletonEnumBean()
  • testSingletonBean()

Error messages for both failed tests in the second run:

java.lang.AssertionError: [someString, someInt] expected:<1> but was:<2>

Proposed Fix

Add a tearDown() method to restore the instances of SingletonEnum and Singleton to default states.

@stleary

stleary commented Apr 26, 2024

Copy link
Copy Markdown
Owner

@kaiyaok2

Copy link
Copy Markdown
Contributor Author

@kaiyaok2 Looks reasonable. How did you come across this issue?

@stleary The issue is reported by our custom JUnit runner that supports rerunning test suite in the same JVM (different from Surefire where a new JVM is booted for each mvn test cycle)

@stleary

stleary commented Apr 26, 2024

Copy link
Copy Markdown
Owner

@stleary stleary merged commit 054786e into stleary:master May 3, 2024
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.

2 participants