Last Updated on 12 June 2020   |   Print Email
In this article, I will guide you how to run JUnit tests in a determined order. You know, when run a whole test class, the execution order of test methods matters if input of a test depends on output of others. For example, when testing CRUD operations, testList() method would depend on testCreate() method – so when running the test class, the testCreate() must run before testList().By default, JUnit executes tests in a specific order of its own, but not predictable. JUnit 5 allows programmers to override that default, to run tests in a determined order: alphanumeric order or numeric order.Suppose that we have a test class as follows:
public class ProductRepositoryTests {
@Test
public void testCreateProduct() {
}
@Test
public void testFindProductByName1() {
}
@Test
public void testFindProductByName2() {
}
@Test
public void testFindProductByName3() {
}
@Test
public void testListProducts() {
}
@Test
public void testUpdateProduct() {
}
@Test
public void testDeleteProduct() {
}
}
Let’s see how to enforce tests order for this test class in JUnit 5.
1. Run Tests in Alphanumeric Order
To run tests in the order of test method names by alphanumeric order, just specify the @TestMethodOrder(Alphanumeric.class) annotation for the test class:
import org.junit.jupiter.api.MethodOrderer.Alphanumeric;
import org.junit.jupiter.api.TestMethodOrder;
@TestMethodOrder(Alphanumeric.class)
public class ProductRepositoryTests {
@Test
public void testCreateProduct() {
}
@Test
public void testFindProductByName1() {
}
@Test
public void testFindProductByName2() {
}
@Test
public void testFindProductByName3() {
}
@Test
public void testListProducts() {
}
@Test
public void testUpdateProduct() {
}
@Test
public void testDeleteProduct() {
}
}
Run this test class and the test methods will be executed in the following order:
Note that JUnit executes tests in alphanumeric order, meaning by ascending order of characters and numbers in method names.
2. Run Tests in Numeric Order
You can run tests by numeric order by using the @TestMethodOrder(OrderAnnotation.class) annotation at class level and specify a number for each test method using the @Order annotation. For example, update the above test class as follows:
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
@TestMethodOrder(OrderAnnotation.class)
public class ProductRepositoryTests {
@Test
@Order(1)
public void testCreateProduct() {
}
@Test
@Order(2)
public void testFindProductByName1() {
}
@Test
@Order(3)
public void testFindProductByName2() {
}
@Test
@Order(4)
public void testFindProductByName3() {
}
@Test
@Order(5)
public void testListProducts() {
}
@Test
@Order(6)
public void testUpdateProduct() {
}
@Test
@Order(7)
public void testDeleteProduct() {
}
}
Run this test class and you will see the test methods run in the following order:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments