JUnit 的@TestMethodOrder 注释不起作用
JUnit's @TestMethodOrder annotation not working
我在进行以下集成测试时遇到问题
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() { ... }
}
我希望在 运行 测试时执行顺序为 1、2、3,但由于某种原因,实际执行顺序为 2、3、1。
老实说,我不知道为什么注释不起作用。我正在使用 Spring Boot 2.1.3 和 JUnit 5.4。
来自文档
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() {}
}
您需要正确配置 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
例如,通过使用 Spring 引导,您可以使用 @FixMethodOrder(MethodSorters.JVM) 而不是 @TestMethodOrder(MethodOrderer.OrderAnnotation.class)。
所有测试都是 运行 按它们出现的顺序排列的。
@FixMethodOrder(MethodSorters.JVM)
public class MyTest{
@Test
public void zzzz(){}
@Test
public void cccc(){}
@Test
public void aaaa(){}
@Test
public void bbbb(){}
}
订单执行是:
zzzz()
cccc()
aaaa()
bbbb()
我遇到过同样的问题。但是,我发现问题到底出在我的案子上。错误导入 "Order" class.
错了
import org.springframework.core.annotation.Order;
右一
*import org.junit.jupiter.api.Order;*
此外,请验证以下五个 class 是否正确导入
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
如果您 have/had JUnit 4,请检查注释的导入 @Test
:import org.junit.Test;
对于 JUnit 5 导入应该是:import org.junit.jupiter.api.Test;
这个问题是我的问题
我在进行以下集成测试时遇到问题
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() { ... }
}
我希望在 运行 测试时执行顺序为 1、2、3,但由于某种原因,实际执行顺序为 2、3、1。
老实说,我不知道为什么注释不起作用。我正在使用 Spring Boot 2.1.3 和 JUnit 5.4。
来自文档 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() {}
}
您需要正确配置 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
例如,通过使用 Spring 引导,您可以使用 @FixMethodOrder(MethodSorters.JVM) 而不是 @TestMethodOrder(MethodOrderer.OrderAnnotation.class)。 所有测试都是 运行 按它们出现的顺序排列的。
@FixMethodOrder(MethodSorters.JVM)
public class MyTest{
@Test
public void zzzz(){}
@Test
public void cccc(){}
@Test
public void aaaa(){}
@Test
public void bbbb(){}
}
订单执行是:
zzzz()
cccc()
aaaa()
bbbb()
我遇到过同样的问题。但是,我发现问题到底出在我的案子上。错误导入 "Order" class.
错了
import org.springframework.core.annotation.Order;
右一
*import org.junit.jupiter.api.Order;*
此外,请验证以下五个 class 是否正确导入
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
如果您 have/had JUnit 4,请检查注释的导入 @Test
:import org.junit.Test;
对于 JUnit 5 导入应该是:import org.junit.jupiter.api.Test;
这个问题是我的问题