JUnit @BeforeClass non-static work around for Spring Boot application

Just use @Before (instead of @BeforeClass) (or Spring's @BeforeTransaction (depending on how you initialize the database)). This annotation must been attached to an nonstatic public method.

Of course: @Before run before EACH test case method (not like @BeforeClass that runs only once.) But if you want to run it exactly once, then use an static marker field.

private static boolean initialized = false;
...
@Before
public void initializeDB() { 
   if (!initialized) {
       ... //your db initialization
       initialized = true;
   }
}
---

For JUnit5: Test Execution Order and @TestInstance(Lifecycle.PER_CLASS)

Kotlin example:

@ExtendWith(SpringExtension::class)
@TestInstance(PER_CLASS)
class BeforeInstanceTests {

    private var initialized: String = ""
    private val callList: MutableList<String> = ArrayList()

    @BeforeAll
    fun beforeAllNonStatic() {
        initialized = "initialized"
        assertEquals(0, callList.size)
    }

    @Test
    fun test1() {
        assertEquals("initialized", initialized)
        callList.add("test1")
    }

    @Test
    fun test2() {
        assertEquals("initialized", initialized)
        callList.add("test2")
    }

    @Test
    fun test3() {
        assertEquals("initialized", initialized)
        callList.add("test3")
    }

    @AfterAll
    fun afterAllNonStatic() {
        assertEquals("initialized", initialized)
        assertEquals(3, callList.size)
        assertTrue(callList.contains("test1"))
        assertTrue(callList.contains("test2"))
        assertTrue(callList.contains("test3"))

        callList.clear()
        initialized = ""
    }
}