-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Introduce support for parameterized classes #4342
Conversation
94c3fd8
to
473f4c8
Compare
3b1836d
to
6de9f4e
Compare
This works very well for me. My main use case is to test multiple implementations of the same interface with the same suite of tests: @ParameterizedContainer
@MethodSource("instances")
public class BranchByAbstractionTest {
public static Stream<Arguments> instances() {
return Stream.of(
new FirstImplementation(),
new SecondImplementation()
).map(Arguments::arguments);
}
private final Abstraction abstraction;
public BranchByAbstractionTest(Abstraction abstraction) {
this.abstraction = abstraction;
}
@Test
void somethingAboutTheAbstraction() {
assertEquals(23, abstraction.doSomething());
}
} I also love that constructor injection works 😍 . Being able to use the same arguments providers is really helpful. |
Maybe for future improvements: Would it be possible and an improvement for readability to have the The same argument would probably be valid for regular parameterized tests, i.e. should the presence of an argument provider on a class/field/method be sufficient to make it parameterized without requiring the additional |
I'm glad to hear it! 🙂
If you have a single parameter, you don't have to wrap the arguments in @ParameterizedClass
@MethodSource("instances")
class BranchByAbstractionTest {
static List<Abstraction> instances() {
return List.of(
new FirstImplementation(),
new SecondImplementation()
);
}
private final Abstraction abstraction;
BranchByAbstractionTest(Abstraction abstraction) {
this.abstraction = abstraction;
}
@Test
void somethingAboutTheAbstraction() {
assertEquals(23, abstraction.doSomething());
}
} Or using @ParameterizedClass
@FieldSource("instances")
class BranchByAbstractionTest {
static final List<Abstraction> instances = List.of(
new FirstImplementation(),
new SecondImplementation()
);
private final Abstraction abstraction;
BranchByAbstractionTest(Abstraction abstraction) {
this.abstraction = abstraction;
}
@Test
void somethingAboutTheAbstraction() {
assertEquals(23, abstraction.doSomething());
}
} If you can use Java records: @ParameterizedClass
@FieldSource("instances")
record BranchByAbstractionTest(Abstraction abstraction) {
static final List<Abstraction> instances = List.of(
new FirstImplementation(),
new SecondImplementation()
);
@Test
void somethingAboutTheAbstraction() {
assertEquals(23, abstraction.doSomething());
}
}
I think we would still require |
Team decision: Merge after renaming the annotation to |
Thanks, Marc! I haven't seen |
Overview
This PR introduces
@ParameterizedClass
which builds on@ContainerTemplate
and allows declaring a top-level or@Nested
test class as a parameterized container to be invoked multiple times with different arguments. The same@...Source
annotations as for@ParameterizedTest
may be used to provide arguments via constructor or field injection.Resolves #878.
I hereby agree to the terms of the JUnit Contributor License Agreement.
Definition of Done
@API
annotations