testbench icon indicating copy to clipboard operation
testbench copied to clipboard

@BrowserConfiguration not working with JUnit 5 @Nested annotation

Open mcollovati opened this issue 3 months ago • 0 comments

Currently, Testbench @BrowserConfiguration does not work correctly with JUnit @Nested annotation that is meant to work with not-static inner classes.

The problem is that when BrowserExtension.evaluateExecutionCondition evaluates whether the test should run or not, it tries to compute the requested capabilities; if the test class has a method annotated with @BrowserConfiguration, that method is supposed to be invoked to get the list of capabilities. However, when BrowserExtension.evaluateExecutionCondition is called, the JUnit test context does not yet have a reference to the test class instance (e.g. ExtensionContext.getTestInstance()), so Testbench tries to create a new instance by reflection calling the default no-args constructor, but it fails because it does not exist (inner classes always take the enclosing class instance as parameter).

To reproduce run the following test, that will fail because the @BrowserConfiguration gets not called

public class MyTest {

    @Nested
    class MyNested extends BrowserTestBase {

        private boolean browserConfigInvoked;

        @BrowserConfiguration
        public List<DesiredCapabilities> getBrowserConfiguration() {
            browserConfigInvoked = true;
            return List.of(BrowserUtil.chrome());
        }

        @BrowserTest
        void simpleTest() {
            Assertions.assertTrue(browserConfigInvoked,
                    "Expecting getBrowserConfiguration() to be invoked");
        }
    }
}

If you do the same on a top-level class, it works

public class MyTest extends BrowserTestBase  {

    private boolean browserConfigInvoked;

    @BrowserConfiguration
    public List<DesiredCapabilities> getBrowserConfiguration() {
        browserConfigInvoked = true;
        return List.of(BrowserUtil.chrome());
    }

    @BrowserTest
    void simpleTest() {
        Assertions.assertTrue(browserConfigInvoked,
                "Expecting getBrowserConfiguration() to be invoked");
    }
}

mcollovati avatar Mar 13 '24 07:03 mcollovati