@TestInstance has two modes:

  • LifeCycle.PER_METHOD (DEFAULT) - JUnit creates a new instance of the test-class for each test-method it contains
  • LifeCycle.PER_CLASS - enables us to ask JUnit to create only one instance of the test class and reuse it between tests

1 - Code Examples

@TestInstance(LifeCycle.PER_CLASS)
class UnitTest {

    private String largeContent;

    @BeforeAll
    void setUpFixture() {
        // read the file
		largeContent = read()
    }
}
@TestInstance(LifeCycle.PER_METHOD)
class UnitTest {

    private static String largeContent;

    @BeforeAll
    static void setUpFixture() {
        // read the file
		largeContent = read()
    }
}