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
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.