MockMVC
is ignoring order of @Order
annotation, by the way, naming them in alphabetical order, when second method is named test2_b
, the method become second do be executed as it should be, but in this case, renamed to test2
it was the last one to be executed even though it should be the second one to be executed:
package com.javainuse.boot3security.test;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import com.javainuse.boot3security.repository.DadosPessoaisRepository;
@SpringBootTest
@AutoConfigureMockMvc
class ClassTest {
@Autowired
MockMvc mockMvc;
@MockBean
private DadosPessoaisRepository dadosPessoaisRepository;
@Order(1)
@Test
public void test1_a() throws Exception {
System.out.println("Test1");
}
@Order(2)
@Test
public void test2() throws Exception {
System.out.println("Test2");
}
@Order(3)
@Test
public void test3_c() throws Exception {
System.out.println("Test3");
}
@Order(4)
@Test
public void test4_d() throws Exception {
System.out.println("Test4");
}
}
Output:
Test1
Test3
Test4
Test2
The test method execution order is nothing to do with @MockMVC
. It is controlled by the junit5 which by default is unpredictable. You have to annotate the test class with @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
if you want to control the execution order using @Order
. Without it , @Order
will not have any effect.
It is mentioned in the documentation as follows :
To control the order in which test methods are executed, annotate your test class or test interface with
@TestMethodOrder
and specify the desiredMethodOrderer
implementation.
MethodOrderer.OrderAnnotation
: sorts test methods numerically based on values specified via the@Order
annotation