GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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 #3261

New issue
Copy link
New issue
Copy link
Closed
#3720
Closed
Simplify implementation of self-contained dynamic tests#3261
#3720
Copy link
Labels
component: Jupitertheme: programming modeltype: enhancementup-for-grabs
Milestone
5.11 RC1

Description

@odrotbohm
odrotbohm
opened on Apr 19, 2023
Issue body actions

@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

Activity

Next
  1. github-actions
    added
    3rd-party: IntelliJ IDEA
    java
    status: new
    on Apr 19, 2023
  2. sormuras commented on Apr 19, 2023

    @sormuras
    sormuras
    on Apr 19, 2023
    Member
    More actions

    Executable in interface NamedExecutable<T extends NamedExecutable<T>> extends Named<T>, Executable is https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/function/Executable.html - right?

  3. odrotbohm commented on Apr 19, 2023

    @odrotbohm
    odrotbohm
    on Apr 19, 2023
    Author
    More actions

    Yes, @sbrannen and me brainstormed the interface and additional methods in a 1:1 yesterday.

    Reacted by Christian Stein
  4. sormuras commented on Apr 19, 2023

    @sormuras
    sormuras
    on Apr 19, 2023
    Member
    More actions
    @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;
      }
    }

    🤔

  5. sormuras
    added
    theme: programming model
    status: team discussion
    component: Jupiter
    and removed
    3rd-party: IntelliJ IDEA
    java
    status: new
    on Apr 19, 2023
  6. odrotbohm commented on Apr 19, 2023

    @odrotbohm
    odrotbohm
    on Apr 19, 2023
    Author
    More actions

    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.

  7. sbrannen commented on Apr 19, 2023

    @sbrannen
    sbrannen
    on Apr 19, 2023
    Member
    More actions

    this instanceof Record would have to be replaced with some reflective hacks since we have a Java 8 baseline.

  8. sbrannen
    added
    status: new
    on Apr 19, 2023
  9. 14 remaining items

  10. mobounya
    added a commit that references this issue on Jan 20, 2024

    Document new changes

    43b708f
  11. marcphilipp
    modified the milestones: 5.11 M1, 5.12 on Feb 2, 2024
  12. marcphilipp
    modified the milestones: 5.11 M2, 5.11 M3 on May 17, 2024
  13. marcphilipp commented on Jul 17, 2024

    @marcphilipp
    marcphilipp
    on Jul 17, 2024
    Member
    More actions

    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?

  14. marcphilipp
    added
    status: in progress
    on Jul 17, 2024
  15. odrotbohm commented on Jul 17, 2024

    @odrotbohm
    odrotbohm
    on Jul 17, 2024
    Author
    More actions

    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. 🤔

  16. marcphilipp
    closed this as completedin #3720on Jul 18, 2024
  17. marcphilipp
    closed this as completedin 6828a79on Jul 18, 2024
  18. PhilipIngham
    added a commit that references this issue on Jul 18, 2024

    Introduce DynamicTests generators for Named<Executable> (junit-team#3720

    This issue will close once commit 6828a79 is merged into the 'main' branch.
    6828a79
  19. marcphilipp commented on Jul 19, 2024

    @marcphilipp
    marcphilipp
    on Jul 19, 2024
    Member
    More actions

    @odrotbohm The latest 5.11.0-SNAPSHOT versions in https://oss.sonatype.org/content/repositories/snapshots now have the changes. Looking forward to your feedback! 🙂

  20. odrotbohm commented on Aug 14, 2024

    @odrotbohm
    odrotbohm
    on Aug 14, 2024
    Author
    More actions

    Congrats on the release. The additions work nicely! 🙇

    Reacted by Marc Philipp and Sam Brannen
  21. sbrannen
    removed
    status: in progress
    on Aug 19, 2024
  22. github-actions
    locked as resolved and limited conversation to collaborators on May 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    component: Jupitertheme: programming modeltype: enhancementup-for-grabs

    Type

    No type

    Projects

    No projects

      Milestone

      • 5.11 RC1

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.