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

Inherited test methods don't support their annotations (fixes #1032) #1033

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

ingvard
Copy link

@ingvard ingvard commented Apr 12, 2024

fixes #1032

Context

The getTestMethod method in the io.qameta.allure.junitplatform.AllureJunitPlatformUtils class doesn't function as expected for tests that are inherited. This is because it uses aClass.getDeclaredMethods(), which only operates on the current instance and does not include methods that are inherited. This can cause issues with annotations related to methods, such as descriptions and others.

Revision 1

In this revision, I used the aClass.getMethods() that returns all public methods on a checking class.

Revision 2

However, revision 1 covers one of the test cases when all test methods are public. It doesn't work when:

  1. Methods have package-based visibility that is usually for unit tests.
   public abstract static class ParentTest implements GrandparentTest {
        @Test
        @Description(TEST_DESCRIPTION)
        @Story(INHERITED_TEST_PARENT_STORY)
        @Link(TEST_LINK)
        void parentTest() {}
    }
  1. Methods are members of the interface (as default methods).
 public interface GrandparentTest {
        @Test
        @Description(TEST_DESCRIPTION)
        @Story(INHERITED_TEST_GRANDPARENT_STORY)
        @Link(TEST_LINK)
        default void grandparentTest() {}
    }
  1. Parent class has an upper-class annotation that must be applied to all inheritors.
 @Epic(INHERITED_TEST_EPIC)
    @Feature(INHERITED_TEST_FUTURE)
    public interface GrandparentTest { }

In short, I have to implement two methods placed into ReflectionUtils to recursively collect method and annotation information. These methods are designed as simplified logic of org.springframework.data.util.ReflectionUtils specifically for our needs.

Checklist

@CLAassistant
Copy link

CLAassistant commented Apr 12, 2024

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions bot added the theme:junit-platform JUnit 5 related issue label Apr 12, 2024
@baev baev added the type:bug Something isn't working label Apr 12, 2024
@ingvard ingvard requested a review from baev April 12, 2024 17:10
import java.util.function.Consumer;
import java.util.stream.Stream;

public final class ReflectionUtils {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add javadoc

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add tests

import java.util.stream.Stream;

public final class ReflectionUtils {
private ReflectionUtils() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add throw new IllegalStateException("Do not instance");

@@ -116,7 +117,7 @@ public static Set<Link> getLinks(final Collection<Annotation> annotations) {
* @return discovered labels.
*/
public static Set<Label> getLabels(final AnnotatedElement annotatedElement) {
return getLabels(annotatedElement.getAnnotations());
return getLabels(getAllAnnotations(annotatedElement));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be added to getLinks method as well

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add test

Copy link
Member

@baev baev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a bug:

public interface First {

    default void a() {
    }
}
public interface Second extends First {

    default void b() {
    }
}

then ReflectionUtils.getAllDeclaredMethods(Second.class) will produce duplicate results:

public default void Second.b(),
public default void First.a(),
public default void First.a()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme:core theme:junit-platform JUnit 5 related issue type:bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐞: Inherited test methods don't support their annotations.
3 participants