Skip to content

@BeforeEach & @AfterEach methods are not invoked using extensions registered at the test method level #523

@rweisleder

Description

@rweisleder

A ParameterResolver, which is configured at test-method-level, is not used for the corresponding BeforeEach-method.

Given this test class:

public class DummyTest {

    @BeforeEach
    public void setUp(MyParameter param) {
        System.out.println("setUp with " + param.value);
    }

    @Test
    @ExtendWith(FirstParameterResolver.class)
    public void firstTest(MyParameter param) {
        System.out.println("running with " + param.value);
    }

    @Test
    @ExtendWith(SecondParameterResolver.class)
    public void secondTest(MyParameter param) {
        System.out.println("running with " + param.value);
    }

    // inlined for completeness

    static class MyParameter {

        public String value;

        public MyParameter(String value) {
            this.value = value;
        }

    }

    static class FirstParameterResolver implements ParameterResolver {

        @Override
        public boolean supports(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
            return true;
        }

        @Override
        public Object resolve(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
            return new MyParameter("FirstParameterResolver");
        }

    }

    static class SecondParameterResolver implements ParameterResolver {

        @Override
        public boolean supports(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
            return true;
        }

        @Override
        public Object resolve(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
            return new MyParameter("SecondParameterResolver");
        }

    }

}

I expected this output:

setUp with FirstParameterResolver
running with FirstParameterResolver
setUp with SecondParameterResolver
running with SecondParameterResolver

but got: org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [mypackage.DummyTest$MyParameter arg0] in executable [public void mypackage.DummyTest.setUp(mypackage.DummyTest$MyParameter)]

I also tried to add a @ExtendWith to my BeforeEach-method, but this results in the same exception.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy