jqwik-team / jqwik

Property-Based Testing on the JUnit Platform
http://jqwik.net
Eclipse Public License 2.0
551 stars 63 forks source link

Lifecycle method called twice when subclassing #562

Closed translatenix closed 2 months ago

translatenix commented 2 months ago

I'm using a subclassing pattern to test multiple implementations/configurations of a class.

This pattern works well with JUnit. But with jqwik, the @AfterProperty method gets called twice. Debugging this, it looks like a reflection problem related to subclassing (two @AfterProperty methods are found).

public abstract class ListTest {
  private final List<String> list;

  public ListTest(List<String> list) {
    this.list = list;
  }

  public static class ArrayListTest extends ListTest {
    public ArrayListTest() {
      super(new ArrayList<>());
    }
  }

  public static class LinkedListTest extends ListTest {
    public LinkedListTest() {
      super(new LinkedList<>());
    }
  }

  @AfterProperty
  public void afterProperty() {
    System.out.println("afterProperty");
  }

  @Example
  public void example() {
    System.out.println("example");
  }
}
jlink commented 2 months ago

@translatenix Thanks for the catch. Which jqwik version(s) have you tried?

translatenix commented 2 months ago

jqwik 1.8.4.

jlink commented 2 months ago

Can confirm as bug.

Workaround: Pull concrete classes outside of the abstract class' compilation scope:

public abstract class ListTest {
  private final List<String> list;

  public ListTest(List<String> list) {
    this.list = list;
  }

  @AfterProperty
  public void afterProperty() {
    System.out.println("afterProperty");
  }

  @Example
  public void example() {
    System.out.println("example");
  }
}

class ArrayListTest extends ListTest {
    public ArrayListTest() {
      super(new ArrayList<>());
    }
}

class LinkedListTest extends ListTest {
    public LinkedListTest() {
      super(new LinkedList<>());
    }
}
jlink commented 2 months ago

Fixed and released in 1.8.5-SNAPSHOT.

@translatenix Please re-open if fix doesn't work for you.