Convert tests from TestNG to JUnit 5#4259
Open
evanchooly wants to merge 14 commits into
Open
Conversation
- 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
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
@BeforeEach,@AfterEach,@BeforeAll,@AfterAll,@Disabled,@Tag,@DisplayName,@ParameterizedTest,@MethodSource)assertEquals/assertNotEqualsargument order from TestNG convention(actual, expected)to JUnit 5 convention(expected, actual)@DataProvidermethods tostatic Stream<Arguments>withArguments.of(...)rowsexpectedExceptionstest bodies inassertThrows()blocksthrow new SkipException(...)withAssumptions.assumeTrue(false, ...)@TestInstance(Lifecycle.PER_CLASS)toTemplatedTestBaseso@AfterAllworks on instance methods (annotation is@Inherited, covers all 261 subclasses)TemplatedTestBase.validateTestName()to read@DisplayNameinstead of TestNG's@Test.testName()assertEquals(double[], double[], delta)→assertArrayEqualsinDoubleMappingTest/FloatMappingTest@Testannotations; add@Testto individual test methods where neededpom.xmlfiles: remove TestNG dependency, add JUnit Jupiter 5.11.4 + params<useModulePath>false</useModulePath>to maven-surefire-pluginTest plan
./mvnw install -pl :morphia-core -DskipTestscompiles cleanly./mvnw test -pl :morphia-core -Dtest="TypesTest"passes (44 parameterized tests)./mvnw install -pl :examples -DskipTestscompiles cleanly./mvnw install -pl :morphia-validation -DskipTestscompiles cleanly./mvnw install -pl :morphia-kotlin -DskipTestscompiles cleanlyhttps://claude.ai/code/session_01FHUBbAt2yQbG2nB5fNwsnz
Generated by Claude Code