JUnit does not guarantee the execution order of the methods defined by the @Test annotation.
Therefore, create the following three classes to control the execution order.
As a process flow, ʻOrderedRunner was added to each test method when executing the test. The flow is to control the execution order by reading the setting value of ʻOrder.
TestMethod.java
public enum TestMethod {
strArrayIsNullTest,
strIsNullTest
};
Order.java
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Order {
TestMethod order();
}
OrderedRunner.java
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
public class OrderedRunner extends BlockJUnit4ClassRunner {
public OrderedRunner(Class<?> klass) throws InitializationError {
super(klass);
}
@Override
protected List<FrameworkMethod> computeTestMethods() {
List<FrameworkMethod> list = super.computeTestMethods();
Collections.sort(list, new Comparator<FrameworkMethod>() {
@Override
public int compare(FrameworkMethod f1, FrameworkMethod f2) {
Order o1 = f1.getAnnotation(Order.class);
Order o2 = f2.getAnnotation(Order.class);
if (o1 == null || o2 == null){
return -1;
}
return o1.order().ordinal() - o2.order().ordinal();
}
});
return list;
}
}
Foo.java
public class Foo {
public static boolean isNull(String[] arg) {
return arg == null;
}
public static boolean isNull(String str) {
return str == null || str.length() == 0 || str.equals("");
}
}
FooTest.java
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(OrderedRunner.class)
public class FooTest {
@Test
@Order(order=TestMethod.strArrayIsNullTest)
public void strArrayIsNullTest() {
String[] arg = {"test"};
assertFalse(Foo.isNull(arg));
System.out.println("strArrayIsNullTest OK");
}
//Even if you add a test, the order is guaranteed by adding an element to TestMethod.
/**
* @Test
* @Order(order=TestMethod.reservationsTest)
* public void reservationsTest();
*/
@Test
@Order(order=TestMethod.strIsNullTest)
public void strIsNullTest() {
String str = "test";
assertFalse(Foo.isNull(str));
System.out.println("strIsNullTest OK");
}
}
Reference material
Recommended Posts