I want to execute test methods which are annotated by @Test
in specific order.
For example:
public class MyTest {
@Test public void
If the order is important, you should make the order yourself.
@Test public void test1() { ... }
@Test public void test2() { test1(); ... }
In particular, you should list some or all possible order permutations to test, if necessary.
For example,
void test1();
void test2();
void test3();
@Test
public void testOrder1() { test1(); test3(); }
@Test(expected = Exception.class)
public void testOrder2() { test2(); test3(); test1(); }
@Test(expected = NullPointerException.class)
public void testOrder3() { test3(); test1(); test2(); }
Or, a full test of all permutations:
@Test
public void testAllOrders() {
for (Object[] sample: permute(1, 2, 3)) {
for (Object index: sample) {
switch (((Integer) index).intValue()) {
case 1: test1(); break;
case 2: test2(); break;
case 3: test3(); break;
}
}
}
}
Here, permute()
is a simple function which iterates all possible permuations into a Collection of array.
Please check out this one: https://github.com/TransparentMarket/junit. It runs the test in the order they are specified (defined within the compiled class file). Also it features a AllTests suite to run tests defined by sub package first. Using the AllTests implementation one can extend the solution in also filtering for properties (we used to use @Fast annotations but those were not published yet).
What you want is perfectly reasonable when test cases are being run as a suite.
Unfortunately no time to give a complete solution right now, but have a look at class:
org.junit.runners.Suite
Which allows you to call test cases (from any test class) in a specific order.
These might be used to create functional, integration or system tests.
This leaves your unit tests as they are without specific order (as recommended), whether you run them like that or not, and then re-use the tests as part of a bigger picture.
We re-use/inherit the same code for unit, integration and system tests, sometimes data driven, sometimes commit driven, and sometimes run as a suite.
Migration to TestNG seems the best way, but I see no clear solution here for jUnit. Here is most readable solution / formatting I found for jUnit:
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SampleTest {
@Test
void stage1_prepareAndTest(){};
@Test
void stage2_checkSomething(){};
@Test
void stage2_checkSomethingElse(){};
@Test
void stage3_thisDependsOnStage2(){};
@Test
void callTimeDoesntMatter(){}
}
This ensures stage2 methods are called after stage1 ones and before stage3 ones.
I've read a few answers and agree its not best practice, but the easiest way to order your tests - and the way that JUnit runs tests by default is by alphabetic name ascending.
So just name your tests in the alphabetic order that you want. Also note the test name must begin with the word test. Just watch out for numbers
test12 will run before test2
so:
testA_MyFirstTest testC_ThirdTest testB_ATestThatRunsSecond
With JUnit 5.4, you can specify the order :
@Test
@Order(2)
public void sendEmailTestGmail() throws MessagingException {
you just need to annotate your class
@TestMethodOrder(OrderAnnotation.class)
https://junit.org/junit5/docs/current/user-guide/#writing-tests-test-execution-order
i'm using this in my project and it works very well !