In JUnit 5, how to run code before all tests

后端 未结 5 2097
悲&欢浪女
悲&欢浪女 2020-12-03 00:34

The @BeforeAll annotation marks a method to run before all tests in a class.

http://junit.org/junit5/docs/current/user-guide/#writing-tests-ann

5条回答
  •  無奈伤痛
    2020-12-03 00:56

    You can mark each of your test classes that uses your database with an interface that defines a static BeforeAll (so that it cannot be overridden). e.g.:

    interface UsesDatabase {
        @BeforeAll
        static void initializeDatabaseConnections() {
            // initialize database connections
        }
    }
    

    This method will be invoked once for each implementing class so you will need to define a way to initialize your connections only once and then do nothing for the other calls.

提交回复
热议问题