-
Notifications
You must be signed in to change notification settings - Fork 542
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
[SUREFIRE-2276] - Fix for retries of JUnit TestTemplate tests #788
Closed
Closed
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5f3d378
Sample code for Junit5's TestContext reporting bug
hubertgrzeskowiak 01faf7f
Parametrised pom and failing test case
hubertgrzeskowiak 8aaf0a6
Fix for test template retries. Also added test for parametrised tests
hubertgrzeskowiak 4f52c2d
Fix for compilation on Java 8 and older Junit versions
hubertgrzeskowiak 4cb82fe
Merge remote-tracking branch 'github/master'
hubertgrzeskowiak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
55 changes: 55 additions & 0 deletions
55
surefire-its/src/test/resources/junit5-testtemplate-bug/pom.xml
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.apache.maven.plugins.surefire</groupId> | ||
<artifactId>surefire-junit-testtemplate-retry-bug</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<name>Test for JUnit 5 TestTemplate with retries</name> | ||
|
||
<properties> | ||
<maven.compiler.source>${java.specification.version}</maven.compiler.source> | ||
<maven.compiler.target>${java.specification.version}</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-engine</artifactId> | ||
<version>${junit5.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<version>${junit5.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-params</artifactId> | ||
<version>${junit5.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.1</version> | ||
<configuration> | ||
<encoding>UTF-8</encoding> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>${surefire.version}</version> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
63 changes: 63 additions & 0 deletions
63
...re-its/src/test/resources/junit5-testtemplate-bug/src/test/java/pkg/FieldSettingTest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package pkg; | ||
|
||
import org.junit.jupiter.api.TestTemplate; | ||
import org.junit.jupiter.api.extension.BeforeEachCallback; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.api.extension.Extension; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.TestTemplateInvocationContext; | ||
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class FieldSettingTest { | ||
private int testValue = 42; | ||
|
||
// We're calling this in the provider underneath | ||
public void setTestValue(int testValue) { | ||
this.testValue = testValue; | ||
} | ||
|
||
@TestTemplate | ||
@ExtendWith(FieldSettingContextProvider.class) | ||
public void testTemplatePartiallyFails() { | ||
assertEquals(42, testValue); | ||
} | ||
} | ||
|
||
|
||
class FieldSettingContextProvider implements TestTemplateInvocationContextProvider { | ||
@Override | ||
public boolean supportsTestTemplate(ExtensionContext extensionContext) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext extensionContext) { | ||
return Stream.of(context(0), context(42)); | ||
} | ||
|
||
private TestTemplateInvocationContext context(int parameter) { | ||
return new TestTemplateInvocationContext() { | ||
@Override | ||
public String getDisplayName(int invocationIndex) { | ||
return String.format("[%d] %s", invocationIndex, parameter); | ||
} | ||
|
||
@Override | ||
public List<Extension> getAdditionalExtensions() { | ||
return getBeforeEachCallbacks(parameter); | ||
} | ||
}; | ||
} | ||
|
||
private List<Extension> getBeforeEachCallbacks(int value) { | ||
return Arrays.asList((BeforeEachCallback) ctx -> | ||
((FieldSettingTest) ctx.getRequiredTestInstance()).setTestValue(value) | ||
); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...e-its/src/test/resources/junit5-testtemplate-bug/src/test/java/pkg/ParamsContextTest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package pkg; | ||
|
||
import org.junit.jupiter.api.TestTemplate; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.api.extension.Extension; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.ParameterContext; | ||
import org.junit.jupiter.api.extension.ParameterResolver; | ||
import org.junit.jupiter.api.extension.TestTemplateInvocationContext; | ||
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class ParamsContextTest { | ||
|
||
@TestTemplate | ||
@ExtendWith(ParamsContextProvider.class) | ||
void testTemplatePartiallyFails(int value) { | ||
assertEquals(42, value); | ||
} | ||
} | ||
|
||
class ParamsContextProvider implements TestTemplateInvocationContextProvider { | ||
|
||
@Override | ||
public boolean supportsTestTemplate(ExtensionContext context) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext context) { | ||
return Stream.of(invocationContext(0), invocationContext(42)); | ||
} | ||
|
||
private TestTemplateInvocationContext invocationContext(int parameter) { | ||
return new TestTemplateInvocationContext() { | ||
@Override | ||
public String getDisplayName(int invocationIndex) { | ||
return String.format("[%d] %s", invocationIndex, parameter); | ||
} | ||
|
||
@Override | ||
public List<Extension> getAdditionalExtensions() { | ||
return Collections.singletonList(new ParameterResolver() { | ||
@Override | ||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
return parameter; | ||
} | ||
}); | ||
} | ||
}; | ||
} | ||
} |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work only for numeric parameters?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the good question!
A test template function gets invoked within a loop (stream to be precise), once for each invocation context. This legacy name contains the index of the invocation and is unrelated to any params the method actually receives, and even unrelated to the display name we set in the invocation context.
I just also double checked by changing the integers used in the test fixtures to strings, and the tests pass just fine.