Junit before class ( non static )

后端 未结 8 643
旧巷少年郎
旧巷少年郎 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:22

    If you don't want to set up static initializers for one time initialization and are not particular about using JUnit, take a look at TestNG. TestNG supports non-static, one-time initialization with a variety of configuration options, all using annotations.

    In TestNG, this would be equivalent to:

    @org.testng.annotations.BeforeClass
    public void setUpOnce() {
       // One time initialization.
    }
    

    For teardown,

    @org.testng.annotations.AfterClass
    public void tearDownOnce() {
       // One time tear down.
    }
    

    For the TestNG equivalent of JUnit 4's @Before and @After, you can use @BeforeMethod and @AfterMethod respectively.

提交回复
热议问题