本文介紹了JUnit的@TestMethodOrder批注不起作用的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
我在進(jìn)行以下集成測(cè)試時(shí)遇到問(wèn)題
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
@SpringBootTest
@ActiveProfiles("test")
@TestMethodOrder(OrderAnnotation.class)
public class FooServiceIT {
@Test
@Order(1)
void testUploadSuccess() { ... }
@Test
@Order(2)
void testDownloadSuccess() { ... }
@Test
@Order(3)
void testDeleteSuccess() { ... }
}
運(yùn)行測(cè)試時(shí),我預(yù)計(jì)執(zhí)行順序?yàn)?、2、3,但由于某種原因,實(shí)際執(zhí)行順序?yàn)?、3、1。
tbh,我不知道為什么注釋不起作用。我使用的是帶有JUnit5.4的Spring Boot 2.1.3。
推薦答案
您需要正確配置您的集成開(kāi)發(fā)環(huán)境。
要求
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
</dependency>
請(qǐng)勿使用提供您的IDE的JUnit5。如果將其添加為庫(kù),您將獲得:
No tests found for with test runner 'JUnit 5'
==================== and this exception ===================
TestEngine with ID 'junit-vintage' failed to discover tests
java.lang.SecurityException: class "org.junit.jupiter.api.TestMethodOrder"'s signer information does not match signer information of other classes in the same package
因此,只需包含提到的依賴(lài)項(xiàng),您的代碼就會(huì)按預(yù)期運(yùn)行:
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class FooServiceIT {
@Test
@Order(1)
public void testUploadSuccess() {
System.out.println("1");
}
@Test
@Order(2)
public void testDownloadSuccess() {
System.out.println("2");
}
@Test
@Order(3)
public void testDeleteSuccess() {
System.out.println("3");
}
}
JUnit結(jié)果:
1
2
3
這篇關(guān)于JUnit的@TestMethodOrder批注不起作用的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,






