GeezerVison is showing github.com — Simplify implementation of self-contained dynamic tests · Issue #3261 · junit-team/junit-framework
Simplify implementation of self-contained dynamic tests #3261Description@TestFactorys and DynamicTests provide API to implement multiple test probes that will become individual tests. They're centered around the idea that one would create test fixtures as instances of types and provide API to then define the naming of the test and the actual assertion:
class MyTests
@TestFactory
Stream<DynamicTest> foo() {
var fixture = Stream.of(new Foo(1, 1, 2), new Foo(2, 3, 5));
return DynamicTest.stream(fixture, it -> assertThat(it.left() + it.right()).isEqualTo(it.result()));
}
record Foo(int left, int right, int result) implements Named<Foo> {
@Override
public String getName() {
return left + " + " + right + " = " + result;
}
@Override
public Foo getPayload() {
return this;
}
}
}
There are a few things to note here:
- We need to implement
getPayload() to return the instance itself to satisfy Named although that method doesn't play into the signature of DynamicTest.stream(…) as the elements of the stream are handed into the callable.
- It would be nice if we were able to colocate the assertion callback with the fixture type so that the implementation of the test factory would only have to return a stream of fixtures.
Assuming an extension of Named looking like this existed:
interface NamedExecutable<T extends NamedExecutable<T>> extends Named<T>, Executable {
@Override
public default T getPayload() {
return (T) this;
}
}
a bit of syntactical sugar on DynamicTest:
@SafeVarargs
public static <T extends NamedExecutable<T>> Stream<DynamicTest> stream(
NamedExecutable<T>... inputGenerator) {
return stream(Arrays.stream(inputGenerator));
}
public static <T extends NamedExecutable<T>> Stream<DynamicTest> stream(
Stream<? extends NamedExecutable<T>> inputGenerator) {
Preconditions.notNull(inputGenerator, "inputGenerator must not be null");
return DynamicTest.stream(inputGenerator, it -> it.execute());
}
Would allow us to reduce the very first code sample to:
public class DummyTest {
@TestFactory
Stream<DynamicTest> foo() {
return DynamicTest.stream(new AdderTest(1, 1, 2), new AdderTest(2, 3, 5));
}
record AdderTest(int left, int right, int result) implements NamedExecutable<AdderTest> {
@Override
public String getName() {
return left + " + " + right + " = " + result;
}
@Override
public void execute() throws Throwable {
assertThat(left + right).isEqualTo(result);
}
}
}
This allows to define parameterize test fixtures nicely and colocate the assertions within a type and the test factory method's responsibility is reduced to set up the individual probes. Reacted by Christian Stein and Rene Leonhardt Activitysormuras commented on Apr 19, 2023 odrotbohm commented on Apr 19, 2023 Yes, @sbrannen and me brainstormed the interface and additional methods in a 1:1 yesterday. Reacted by Christian Stein sormuras commented on Apr 19, 2023 @FunctionalInterface
interface NamedExecutable<T extends NamedExecutable<T>> extends Named<T>, Executable {
@Override
public String getName() {
return this instanceof Record ? toString() : "Override getName() to generate a better name for " + this;
}
@Override
public default T getPayload() {
return (T) this;
}
}
🤔 odrotbohm commented on Apr 19, 2023 Great idea. I usually define an explicit name as I value properly readable test names, but I agree that a record's toString() would be a sensible default. sbrannen commented on Apr 19, 2023 this instanceof Record would have to be replaced with some reflective hacks since we have a Java 8 baseline.
marcphilipp commented on Jul 17, 2024 I've simplified the proposed implementation in #3720 to remove the self type parameter.
Instead of
interface NamedExecutable<T extends NamedExecutable<T>> extends Named<T>, Executable {
@SuppressWarnings("unchecked")
@Override
public default T getPayload() {
return (T) this;
}
}
it's now
interface NamedExecutable extends Named<Executable>, Executable {
@Override
public default Executable getPayload() {
return this;
}
}
which is simpler to implement.
Moreover, I've changed the signature of the new DynamicTest.stream method to be more generic so it allows for other Named<E> implementations where E extends Executable:
<T extends Named<E>, E extends Executable> Stream<DynamicTest> stream(
Stream<? extends T> inputStream) {...}
@odrotbohm @sbrannen @sormuras @juliette-derancourt @mmerdes @mobounya Any objections? odrotbohm commented on Jul 17, 2024 Is that something I would be able to find in some snapshots already? I see a few commits related to the ticket but can't find the described code in 5.11 snapshots. 🤔 added a commit that references this issue on Jul 18, 2024This issue will close once commit 6828a79 is merged into the 'main' branch.6828a79 marcphilipp commented on Jul 19, 2024 odrotbohm commented on Aug 14, 2024 Reacted by Marc Philipp and Sam Brannen locked as resolved and limited conversation to collaborators on May 4, 2026
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|