MockMVC
忽略了@Order
注解的顺序,顺便说一下,按字母顺序命名它们,当第二个方法命名为test2_b
时,该方法将成为第二个执行的方法,但在这种情况下,重命名为test2
,它是最后一个执行的方法,尽管它应该是第二个执行的方法:
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");
}
}
输出量:
Test1
Test3
Test4
Test2
2条答案
按热度按时间rpppsulh1#
测试方法的执行顺序与
@MockMVC
无关。它由junit5控制,默认情况下是不可预测的。如果您想使用@Order
控制执行顺序,则必须使用@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
注解测试类。如果没有它,@Order
将不会有任何效果。在文档中提到如下:
要控制测试方法的执行顺序,请使用
@TestMethodOrder
注解测试类或测试接口,并指定所需的MethodOrderer
实现。MethodOrderer.OrderAnnotation
:根据通过@Order
注解指定的值对测试方法进行数字排序pgvzfuti2#
仅在测试方法上使用@Order注解是不起作用的。您必须使用特定的TestMethodOrder来注解Test类:
但请记住,强制执行测试命令不是一个好的做法。