Skip to content
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

Lifecycle method called twice when subclassing #562

Closed
odenix opened this issue Apr 13, 2024 · 4 comments
Closed

Lifecycle method called twice when subclassing #562

odenix opened this issue Apr 13, 2024 · 4 comments
Labels

Comments

@odenix
Copy link

odenix commented Apr 13, 2024

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
Copy link
Collaborator

jlink commented Apr 13, 2024

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

@odenix
Copy link
Author

odenix commented Apr 13, 2024

jqwik 1.8.4.

@jlink jlink added the bug label Apr 14, 2024
@jlink
Copy link
Collaborator

jlink commented Apr 14, 2024

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
Copy link
Collaborator

jlink commented Apr 14, 2024

Fixed and released in 1.8.5-SNAPSHOT.

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

@jlink jlink closed this as completed Apr 14, 2024
@jlink jlink removed the in progress label Apr 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants