Junit before class ( non static )

后端 未结 8 678
旧巷少年郎
旧巷少年郎 2020-12-02 12:12

Are there any best practices to get Junit execute a function once in a test file , and it should also not be static.

like @BeforeClass on non static fun

8条回答
  •  被撕碎了的回忆
    2020-12-02 12:24

    Just use @BeforeClass:

    @BeforeClass
    public static void init() {
    }
    

    It doesn't make sense for init to be non-static because each test is run in a separate instance. The instance that init is run on would not match the instance of any test.

    The only reason that you might want it to be non-static is to override it in subclasses, but you can do this with static methods too. Just use the same name, and only the subclass init method will be called.

提交回复
热议问题