简体   繁体   English

如何创建在所有大型类中都具有嵌套的类之前仅运行一次的扩展

[英]How to create Extension which runs beforeAll only once for whole big class with many nested classes inside

Given my tested class with many nested classes inside: 给定我的测试类,其中包含许多嵌套类:

class TestWithManyNested {

    @RegisterExtension
    static MyExtension extension = new MyExtension();

    @Nested
    class Nested1 {

        @Test
        void nested1() {
        }
    }

    @Nested
    class Nested2 {

        @Test
        void nested2() {
        }
    }
}

Here is my simple extension: 这是我的简单扩展名:

static class MyExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback {

        @Override
        public void beforeAll(ExtensionContext extensionContext) throws Exception {
            System.out.println("beforeAll");
        }

        @Override
        public void afterAll(ExtensionContext extensionContext) throws Exception {
            System.out.println("afterAll");
        }
    }

How can I run my TestWithManyNested with MyExtension which just run beforeAll only once for whole test 我怎样才能运行我TestWithManyNestedMyExtension刚刚运行beforeAll对整个测试只有一次

If you only want the class-level callbacks to execute for the outermost class, you can rewrite your extension as follows (checking that there is no "enclosing class" for the outermost class). 如果只希望对最外层的类执行类级回调,则可以按以下方式重写扩展名(检查最外层的类是否没有“封闭类”)。

static class MyExtension implements BeforeAllCallback, AfterAllCallback {

    @Override
    public void beforeAll(ExtensionContext extensionContext) throws Exception {
        Class<?> testClass = extensionContext.getRequiredTestClass();
        if (testClass.getEnclosingClass() == null) {
            System.err.println("beforeAll: " + testClass.getName());
        }
    }

    @Override
    public void afterAll(ExtensionContext extensionContext) throws Exception {
        Class<?> testClass = extensionContext.getRequiredTestClass();
        if (testClass.getEnclosingClass() == null) {
            System.err.println("afterAll: " + testClass.getName());
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何运行一个可以运行多个Selenium类的类? - How to run a class which runs multiple Selenium classes? 对于每个嵌套的测试类,都会执行 junit5 扩展的 BeforeAll / AfterAll 回调。 这是预期的吗? - BeforeAll / AfterAll callbacks of junit5 extension are executed for each nested test class. Is this expected? JUnit如何仅在测试类内部定义一次的情况下管理运行@BeforeClass - How JUnit manages running @BeforeClass only once which is defined inside the test class 类可以嵌套在类中多少次? - How many times can classes be nested within a class? 如何不创建许多仅用于序列化嵌套 JSON 有效负载的类? - How to not create many classes just for serialising nested JSON payload? 创建类对象的类 - Class which create object of classes 如何创建实现两个通用接口(不修改这些类)的许多类的实例的集合/列表? - How to create a collection/list of instances of many classes which implement two common interfaces (without modifying those classes)? 如何在Kotlin中创建对类进行操作的扩展功能? - How do I create an extension function in Kotlin, which operates on a class? ScheduledExecutorService 只运行一次 - ScheduledExecutorService only runs once 线程只运行一次 - Thread runs only once
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM