-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit introduces two new annotations for ``@ParameterizedClass`-specific lifecycle methods. Methods annotated with ``@BeforeArgumentSet` or ``@AfterArgumentSet` are called once before or after, respectively, each argument set the parameterized class is invoked with. Depending on their `injectArguments` annotation attribute, they may consume the invocation's arguments, for example, to initialize them. Resolves #4352.
- Loading branch information
1 parent
2e27ea7
commit 1e1f8d5
Showing
21 changed files
with
2,154 additions
and
421 deletions.
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
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
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
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
101 changes: 101 additions & 0 deletions
101
documentation/src/test/java/example/ParameterizedMigrationDemo.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,101 @@ | ||
/* | ||
* Copyright 2015-2025 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package example; | ||
|
||
import java.util.Arrays; | ||
|
||
import org.junit.jupiter.params.AfterArgumentSet; | ||
import org.junit.jupiter.params.BeforeArgumentSet; | ||
import org.junit.jupiter.params.ParameterizedClass; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
public class ParameterizedMigrationDemo { | ||
|
||
@SuppressWarnings("JUnitMalformedDeclaration") | ||
// tag::before[] | ||
@RunWith(Parameterized.class) | ||
// end::before[] | ||
static | ||
// tag::before[] | ||
public class JUnit4ParameterizedClassTests { | ||
|
||
@Parameterized.Parameters | ||
public static Iterable<Object[]> data() { | ||
return Arrays.asList(new Object[][] { { 1, "foo" }, { 2, "bar" } }); | ||
} | ||
|
||
// end::before[] | ||
@SuppressWarnings("DefaultAnnotationParam") | ||
// tag::before[] | ||
@Parameterized.Parameter(0) | ||
public int number; | ||
|
||
@Parameterized.Parameter(1) | ||
public String text; | ||
|
||
@Parameterized.BeforeParam | ||
public static void before(int number, String text) { | ||
} | ||
|
||
@Parameterized.AfterParam | ||
public static void after() { | ||
} | ||
|
||
@org.junit.Test | ||
public void someTest() { | ||
} | ||
|
||
@org.junit.Test | ||
public void anotherTest() { | ||
} | ||
} | ||
// end::before[] | ||
|
||
@SuppressWarnings("JUnitMalformedDeclaration") | ||
// tag::after[] | ||
@ParameterizedClass | ||
@MethodSource("data") | ||
// end::after[] | ||
static | ||
// tag::after[] | ||
class JupiterParameterizedClassTests { | ||
|
||
static Iterable<Object[]> data() { | ||
return Arrays.asList(new Object[][] { { 1, "foo" }, { 2, "bar" } }); | ||
} | ||
|
||
@org.junit.jupiter.params.Parameter(0) | ||
int number; | ||
|
||
@org.junit.jupiter.params.Parameter(1) | ||
String text; | ||
|
||
@BeforeArgumentSet(injectArguments = true) | ||
static void before(int number, String text) { | ||
} | ||
|
||
@AfterArgumentSet | ||
static void after() { | ||
} | ||
|
||
@org.junit.jupiter.api.Test | ||
void someTest() { | ||
} | ||
|
||
@org.junit.jupiter.api.Test | ||
void anotherTest() { | ||
} | ||
} | ||
// end::after[] | ||
|
||
} |
93 changes: 93 additions & 0 deletions
93
documentation/src/test/java21/example/ParameterizedLifecycleDemo.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,93 @@ | ||
/* | ||
* Copyright 2015-2025 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package example; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
import org.junit.jupiter.params.AfterArgumentSet; | ||
import org.junit.jupiter.params.BeforeArgumentSet; | ||
import org.junit.jupiter.params.Parameter; | ||
import org.junit.jupiter.params.ParameterizedClass; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
public class ParameterizedLifecycleDemo { | ||
|
||
@Nested | ||
// tag::example[] | ||
@ParameterizedClass | ||
@MethodSource("textFiles") | ||
class TextFileTests { | ||
|
||
static List<TextFile> textFiles() { | ||
return List.of( | ||
// tag::custom_line_break[] | ||
new TextFile("file1", "first content"), | ||
// tag::custom_line_break[] | ||
new TextFile("file2", "second content") | ||
// tag::custom_line_break[] | ||
); | ||
} | ||
|
||
@Parameter | ||
TextFile textFile; | ||
|
||
@BeforeArgumentSet(injectArguments = true) | ||
static void beforeArgumentSet(TextFile textFile, @TempDir Path tempDir) throws Exception { | ||
var filePath = tempDir.resolve(textFile.fileName); // <1> | ||
textFile.path = Files.writeString(filePath, textFile.content); | ||
} | ||
|
||
@AfterArgumentSet(injectArguments = true) | ||
static void afterArgumentSet(TextFile textFile) throws Exception { | ||
var actualContent = Files.readString(textFile.path); // <3> | ||
assertEquals(textFile.content, actualContent, "Content must not have changed"); | ||
// Custom cleanup logic, if necessary | ||
// File will be deleted automatically by @TempDir support | ||
} | ||
|
||
@Test | ||
void test() { | ||
assertTrue(Files.exists(textFile.path)); // <2> | ||
} | ||
|
||
@Test | ||
void anotherTest() { | ||
// ... | ||
} | ||
|
||
static class TextFile { | ||
|
||
final String fileName; | ||
final String content; | ||
Path path; | ||
|
||
TextFile(String fileName, String content) { | ||
this.fileName = fileName; | ||
this.content = content; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return fileName; | ||
} | ||
} | ||
} | ||
// end::example[] | ||
|
||
} |
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
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
Oops, something went wrong.