Skip to content

Convert tests from TestNG to JUnit 5#4259

Open
evanchooly wants to merge 14 commits into
masterfrom
claude/convert-testng-to-junit-cJoaO
Open

Convert tests from TestNG to JUnit 5#4259
evanchooly wants to merge 14 commits into
masterfrom
claude/convert-testng-to-junit-cJoaO

Conversation

@evanchooly
Copy link
Copy Markdown
Member

Summary

  • Replace all TestNG annotations with JUnit 5 equivalents (@BeforeEach, @AfterEach, @BeforeAll, @AfterAll, @Disabled, @Tag, @DisplayName, @ParameterizedTest, @MethodSource)
  • Swap assertEquals/assertNotEquals argument order from TestNG convention (actual, expected) to JUnit 5 convention (expected, actual)
  • Convert @DataProvider methods to static Stream<Arguments> with Arguments.of(...) rows
  • Wrap expectedExceptions test bodies in assertThrows() blocks
  • Replace throw new SkipException(...) with Assumptions.assumeTrue(false, ...)
  • Add @TestInstance(Lifecycle.PER_CLASS) to TemplatedTestBase so @AfterAll works on instance methods (annotation is @Inherited, covers all 261 subclasses)
  • Update TemplatedTestBase.validateTestName() to read @DisplayName instead of TestNG's @Test.testName()
  • Fix array equality: assertEquals(double[], double[], delta)assertArrayEquals in DoubleMappingTest/FloatMappingTest
  • Remove invalid class-level @Test annotations; add @Test to individual test methods where needed
  • Update all pom.xml files: remove TestNG dependency, add JUnit Jupiter 5.11.4 + params
  • Add <useModulePath>false</useModulePath> to maven-surefire-plugin

Test plan

  • ./mvnw install -pl :morphia-core -DskipTests compiles cleanly
  • ./mvnw test -pl :morphia-core -Dtest="TypesTest" passes (44 parameterized tests)
  • ./mvnw install -pl :examples -DskipTests compiles cleanly
  • ./mvnw install -pl :morphia-validation -DskipTests compiles cleanly
  • ./mvnw install -pl :morphia-kotlin -DskipTests compiles cleanly
  • Full test suite run against a MongoDB instance

https://claude.ai/code/session_01FHUBbAt2yQbG2nB5fNwsnz


Generated by Claude Code

- Replace all TestNG annotations (@BeforeMethod, @AfterMethod, @BeforeClass,
  @afterclass, @test, @ignore, etc.) with JUnit 5 equivalents
- Swap assertEquals/assertNotEquals argument order from TestNG (actual, expected)
  to JUnit 5 (expected, actual) convention
- Convert @dataProvider methods to Stream<Arguments> returning static methods
- Replace expectedExceptions attribute with assertThrows() blocks
- Replace SkipException with Assumptions.assumeTrue(false, ...)
- Add @testinstance(PER_CLASS) to TemplatedTestBase for @afterall on instance methods
- Update TemplatedTestBase.validateTestName() to use @DisplayName annotation
- Fix array equality assertions to use assertArrayEquals in DoubleMappingTest/FloatMappingTest
- Add missing assertThrows static imports across validation and geo test files
- Remove invalid class-level @test annotations; add @test to individual test methods
- Update all pom.xml files: remove TestNG, add JUnit Jupiter 5.11.4 dependencies
- Add useModulePath=false to maven-surefire-plugin configuration

https://claude.ai/code/session_01FHUBbAt2yQbG2nB5fNwsnz
Copilot AI review requested due to automatic review settings May 20, 2026 12:19
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot wasn't able to review this pull request because it exceeds the maximum number of files (300). Try reducing the number of changed files and requesting a review from Copilot again.

claude and others added 13 commits May 20, 2026 12:36
The conversion script incorrectly swapped JSONAssert.assertEquals arguments
treating them as TestNG assertions. JSONAssert uses (message, expected, actual)
ordering which differs from both TestNG and JUnit 5 conventions.

https://claude.ai/code/session_01FHUBbAt2yQbG2nB5fNwsnz
@testinstance is not @inherited in Java, so TemplatedTestBase's PER_CLASS
lifecycle did not propagate to its ~261 subclasses. Those subclasses inherited
the non-static @afterall testCoverage() method but used the default PER_METHOD
lifecycle, causing JUnit 5 to throw a configuration error for every test class.

Setting the global default to PER_CLASS matches original TestNG behavior
(one instance per class) and resolves the @afterall instance method constraint.

https://claude.ai/code/session_01FHUBbAt2yQbG2nB5fNwsnz
The junit-platform.properties approach may not be picked up reliably
when surefire:test runs directly in reuseBuild CI mode. Using Surefire's
configurationParameters in pom.xml is the most reliable way to set the
JUnit Jupiter test instance lifecycle default to per_class.

Also adds explicit @testinstance(PER_CLASS) to TestVarHandleAccessor
which has a non-static @BeforeAll and doesn't extend TestBase.

https://claude.ai/code/session_01FHUBbAt2yQbG2nB5fNwsnz
The <properties><configurationParameters> approach in pluginManagement was
not reliably inherited by the core module which has its own surefire config.
Switch to <systemPropertyVariables> which JUnit Platform reads directly and
which is properly merged across the Maven configuration hierarchy.

https://claude.ai/code/session_01FHUBbAt2yQbG2nB5fNwsnz
The @testinstance(PER_CLASS) annotation is not @inherited, so relying on
configuration to apply it globally was fragile. Move testCoverage() logic
into TestCoverageExtension which implements AfterAllCallback — extensions
are invoked regardless of lifecycle mode, making this approach robust.

@ExtendWith IS @inherited so all 261 TemplatedTestBase subclasses get the
extension automatically. The extension handles missing resource folders
(TestTransactions, FiltersTest) and both action.json/expected.json file
naming conventions (QueryTest).

https://claude.ai/code/session_01FHUBbAt2yQbG2nB5fNwsnz
TestTransactions and similar classes have a resource folder but test
methods not named testExample* (e.g., aggregation()). The original
TestNG code had empty testCoverage() overrides to skip this check.
With TestCoverageExtension, skip when no testExample* methods exist.
…for arrays

JUnit 5's assertEquals uses reference equality for arrays, unlike TestNG which
delegated to element-wise comparison. Also fix TestDocumentWriter tests where
BSON Document.equals() rejects MergingDocument (strict class check) by comparing
via toJson() instead.
fixes a failure at the end of the test run about a missing method
@evanchooly evanchooly requested a review from Copilot May 21, 2026 04:32
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot wasn't able to review this pull request because it exceeds the maximum number of files (300). Try reducing the number of changed files and requesting a review from Copilot again.

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.

3 participants