Execute order for test suite in junit

Though quoting @Andy again -

You shouldn't care about test ordering. If it's important, you've got interdependencies between tests, so you're testing behaviour + interdependencies, not simply behaviour. Your tests should work identically when executed in any order.

But if the need be to do so, you can try out Suite

@RunWith(Suite.class)

@Suite.SuiteClasses({
        TestClass2.class,
        TestClass1.class
})
public class JunitSuiteTest {
}

where you can either specify

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestClass1 {

    @AfterClass
    public void testMethod4() {

and then take care to name your method testMethod4 as such to be executed at the end OR you can also use @AfterClass which could soon be replaced by @AfterAll in Junit5.

Do take a look at Controlling the Order of the JUnit test by Alan Harder

Tags:

Java

Junit