我在进行以下集成测试时遇到问题
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
@SpringBootTest
@ActiveProfiles("test")
@TestMethodOrder(OrderAnnotation.classs)
public class FooServiceIT {
@Test
@Order(1)
void testUploadSuccess() { ... }
@Test
@Order(2)
void testDownloadSuccess() { ... }
@Test
@Order(3)
void testDeleteSuccess() { ... }
}
我希望在运行测试时,执行顺序为1、2、3,但由于某种原因,实际的执行顺序为2、3、1。
Tbh,我不知道为什么注释不起作用。我正在将Spring Boot 2.1.3与JUnit 5.4一起使用。
您需要正确配置您的IDE。
需求
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
</dependency>
请勿使用提供您的IDE的JUnit 5。如果将其添加为库,您将获得:
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
因此只包括提到的依赖项,您的代码将按预期工作:
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结果:
1
2
3
您需要正确配置pom.xml。见我的:
<properties>
<!-- Dependency versions -->
<junit.jupiter.version>5.6.0</junit.jupiter.version>
<maven-surefire-plugin.version>3.0.0-M4</maven-surefire-plugin.version>
<!-- Java 10 -->
<maven.compiler.source>1.10</maven.compiler.source>
<maven.compiler.target>1.10</maven.compiler.target>
<!-- Encoding -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<!-- Jupiter API for writing tests -->
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- Maven Surefire plugin to run tests -->
<build>
<plugins>
<!-- plugin to run test cases from maven -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
<!-- Maven plugin to use perticular java version to compile code -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
现在,您的Junit 5注释必须正确运行
根据先前推荐的解决方案应用所有设置后。我仍然有相同的反向或随机@ order执行。
Pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
Maven依赖树:
mvn dependency:tree -Dverbose -Dincludes=org.junit.jupiter:junit-jupiter-engine
[INFO] com.personalitytest.demo:personalitytest:jar:1.0-SNAPSHOT
[INFO] \- org.junit.jupiter:junit-jupiter-engine:jar:5.4.0:test
JUnit测试:
@SpringBootTest
@ActiveProfiles("test")
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class JUnitOrderTest {
private static final Logger log = LoggerFactory.getLogger(JUnitOrderTest.class);
@Test
@Order(1)
public void testUploadSuccess() {
log.info("Junit - Order 1");
}
@Test
@Order(2)
public void testDownloadSuccess() {
log.info("Junit - Order 2");
}
@Test
@Order(3)
public void testDeleteSuccess() {
log.info("Junit - Order 3");
}
}
输出:
Running com.personalitytest.demo.security.JUnitOrderTest
08:48:35.470 [main] INFO com.personalitytest.demo.security.JUnitOrderTest - Junit - Order 2
08:48:35.480 [main] INFO com.personalitytest.demo.security.JUnitOrderTest - Junit - Order 3
08:48:35.481 [main] INFO com.personalitytest.demo.security.JUnitOrderTest - Junit - Order 1
来自文档https://junit.org/junit5/docs/current/api/org/junit/jupiter/api/TestMethodOrder.html
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class OrderedTests {
@Test
@Order(1)
void nullValues() {}
@Test
@Order(2)
void emptyValues() {}
@Test
@Order(3)
void validValues() {}
}