From fb52242afed4b84d4bc169b6e6c7c9f0ee5da4e7 Mon Sep 17 00:00:00 2001 From: Col-E Date: Sun, 3 Nov 2019 17:41:16 -0500 Subject: [PATCH] Fix @ParameterizedTest not being rerun correctly --- .../surefire-junit-platform/pom.xml | 6 +++ .../junitplatform/JUnitPlatformProvider.java | 9 +--- .../junitplatform/RunListenerAdapter.java | 32 +++-------- .../JUnitPlatformProviderTest.java | 54 +++++++++++++++++++ 4 files changed, 68 insertions(+), 33 deletions(-) diff --git a/surefire-providers/surefire-junit-platform/pom.xml b/surefire-providers/surefire-junit-platform/pom.xml index 823885f7ac..e073232b7e 100644 --- a/surefire-providers/surefire-junit-platform/pom.xml +++ b/surefire-providers/surefire-junit-platform/pom.xml @@ -95,6 +95,12 @@ junit-jupiter-engine test + + org.junit.jupiter + junit-jupiter-params + 5.3.2 + test + org.mockito mockito-core diff --git a/surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/JUnitPlatformProvider.java b/surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/JUnitPlatformProvider.java index f9a650d3b5..65ca79ec1b 100644 --- a/surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/JUnitPlatformProvider.java +++ b/surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/JUnitPlatformProvider.java @@ -31,7 +31,7 @@ import static org.apache.maven.surefire.util.TestsToRun.fromClass; import static org.junit.platform.commons.util.StringUtils.isBlank; import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; -import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod; +import static org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId; import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request; import java.io.IOException; @@ -190,12 +190,7 @@ private LauncherDiscoveryRequest buildLauncherDiscoveryRequestForRerunFailures( // Iterate over recorded failures for ( TestIdentifier identifier : new HashSet<>( adapter.getFailures().keySet() ) ) { - // Extract quantified test name data - String[] classMethodName = adapter.toClassMethodNameWithoutPlan( identifier ); - String className = classMethodName[0]; - String methodName = classMethodName[2]; - // Add filter for the specific failing method - builder.selectors( selectMethod( className, methodName ) ); + builder.selectors( selectUniqueId( identifier.getUniqueId() ) ); } return builder.build(); } diff --git a/surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/RunListenerAdapter.java b/surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/RunListenerAdapter.java index f16e2a0cbb..5e61a90211 100644 --- a/surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/RunListenerAdapter.java +++ b/surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/RunListenerAdapter.java @@ -214,16 +214,6 @@ private StackTraceWriter toStackTraceWriter( String realClassName, String realMe return new PojoStackTraceWriter( realClassName, realMethodName, throwable ); } - private String[] toClassMethodName( TestIdentifier testIdentifier ) - { - return toClassMethodName( testIdentifier, true ); - } - - String[] toClassMethodNameWithoutPlan( TestIdentifier testIdentifier ) - { - return toClassMethodName( testIdentifier, false ); - } - /** * * * @param testIdentifier a class or method - * @param usePlan {@code true} for using the test-plan to fetch sources. - * {@code false} to rely on fallbacks. * @return 4 elements string array */ - private String[] toClassMethodName( TestIdentifier testIdentifier, boolean usePlan ) + private String[] toClassMethodName( TestIdentifier testIdentifier ) { Optional testSource = testIdentifier.getSource(); String display = testIdentifier.getDisplayName(); @@ -247,18 +235,10 @@ private String[] toClassMethodName( TestIdentifier testIdentifier, boolean usePl MethodSource methodSource = testSource.map( MethodSource.class::cast ).get(); String realClassName = methodSource.getClassName(); - String[] source; - if ( usePlan ) - { - source = testPlan.getParent( testIdentifier ) - .map( i -> toClassMethodName( i, usePlan ) ) - .map( s -> new String[] { s[0], s[1] } ) - .orElse( new String[] { realClassName, realClassName } ); - } - else - { - source = new String[] { realClassName, realClassName }; - } + String[] source = testPlan.getParent( testIdentifier ) + .map( i -> toClassMethodName( i ) ) + .map( s -> new String[] { s[0], s[1] } ) + .orElse( new String[] { realClassName, realClassName } ); String methodName = methodSource.getMethodName(); boolean useMethod = display.equals( methodName ) || display.equals( methodName + "()" ); @@ -276,7 +256,7 @@ else if ( testSource.filter( ClassSource.class::isInstance ).isPresent() ) } else { - String source = !usePlan ? display : testPlan.getParent( testIdentifier ) + String source = testPlan.getParent( testIdentifier ) .map( TestIdentifier::getDisplayName ).orElse( display ); return new String[] {source, source, display, display}; } diff --git a/surefire-providers/surefire-junit-platform/src/test/java/org/apache/maven/surefire/junitplatform/JUnitPlatformProviderTest.java b/surefire-providers/surefire-junit-platform/src/test/java/org/apache/maven/surefire/junitplatform/JUnitPlatformProviderTest.java index 954bb128f8..1c9c988213 100644 --- a/surefire-providers/surefire-junit-platform/src/test/java/org/apache/maven/surefire/junitplatform/JUnitPlatformProviderTest.java +++ b/surefire-providers/surefire-junit-platform/src/test/java/org/apache/maven/surefire/junitplatform/JUnitPlatformProviderTest.java @@ -296,6 +296,44 @@ public void rerunWithSuccess() assertEquals( 0, summary.getTestsFailedCount() ); } + @Test + public void rerunParameterized() + throws Exception + { + Launcher launcher = LauncherFactory.create(); + ProviderParameters parameters = providerParametersMock(); + // Mock the rerun variable + when( parameters.getTestRequest().getRerunFailingTestsCount() ).thenReturn( 2 ); + when( parameters.getProviderProperties() ) + .thenReturn( singletonMap( JUnitPlatformProvider.CONFIGURATION_PARAMETERS, + "forkCount = 1\nreuseForks = true" ) ); + + JUnitPlatformProvider provider = new JUnitPlatformProvider( parameters, launcher ); + TestPlanSummaryListener executionListener = new TestPlanSummaryListener(); + launcher.registerTestExecutionListeners( executionListener ); + + invokeProvider( provider, TestClass7.class ); + + assertThat( executionListener.summaries ).hasSize( 3 ); + TestExecutionSummary summary = executionListener.summaries.get( 0 ); + assertEquals( 2, summary.getTestsFoundCount() ); + assertEquals( 2, summary.getTestsStartedCount() ); + assertEquals( 1, summary.getTestsSucceededCount() ); + assertEquals( 1, summary.getTestsFailedCount() ); + + summary = executionListener.summaries.get( 1 ); + assertEquals( 1, summary.getTestsFoundCount() ); + assertEquals( 1, summary.getTestsStartedCount() ); + assertEquals( 0, summary.getTestsSucceededCount() ); + assertEquals( 1, summary.getTestsFailedCount() ); + + summary = executionListener.summaries.get( 1 ); + assertEquals( 1, summary.getTestsFoundCount() ); + assertEquals( 1, summary.getTestsStartedCount() ); + assertEquals( 0, summary.getTestsSucceededCount() ); + assertEquals( 1, summary.getTestsFailedCount() ); + } + @Test public void allDiscoveredTestsAreInvokedForNullArgument() throws Exception @@ -921,4 +959,20 @@ void testFailTwice2() assertTrue( count >= 3 ); }*/ } + + static class TestClass7 + { + static List params() + { + return Arrays.asList( new Object[] { "Always pass", true }, + new Object[] { "Always fail", false } ); + } + + @org.junit.jupiter.params.ParameterizedTest + @org.junit.jupiter.params.provider.MethodSource("params") + void testParameterizedTestCases( String testName, boolean value ) + { + assertTrue( value ); + } + } }