前两天用SpringBoot做新项目,发现JUnit用@FixMethodOrder
指定测试顺序无效
package com.task;
import java.util.LinkedList;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.FixMethodOrder;
import org.junit.runners.MethodSorters;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.task.entity.Platform;
import com.task.repository.PlatformRepository;
import junit.framework.TestCase;
@DataJpaTest
@FixMethodOrder(MethodSorters.JVM)
class PlatformRepositoryTest extends TestCase {
@Autowired
private PlatformRepository platformRepository;
//Hope to run in order of creation, selection, and deletion
@Test
public void createTest() {
System.out.println("============ Enter PlatformRepository createTest ===========");
System.out.println("============ Exit PlatformRepository createTest ===========");
}
@Test
public void selectTest() {
System.out.println("============ Enter PlatformRepository selectTest ===========");
System.out.println("============ Exit PlatformRepository selectTest ===========");
}
@Test
public void deleteTest() {
System.out.println("============ Enter PlatformRepository deleteTest ===========");
System.out.println("============ Exit PlatformRepository deleteTest ===========");
}
}
结果
============ Enter PlatformRepository selectTest ===========
============ Exit PlatformRepository selectTest ===========
============ Enter PlatformRepository deleteTest ===========
============ Exit PlatformRepository deleteTest ===========
============ Enter PlatformRepository createTest ===========
============ Exit PlatformRepository createTest ===========
百度上有人指出可能是Junit版本问题,但是没有具体信息
总之先查看一下pom.xml里junit的版本依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
查阅资料后发现这个依赖表示junit5, junit4是没有<exclusions>
部分的,或者像下面这样
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<version>4.12</version>
</dependency>
查阅官网也可以证实MethodSorters
是junit4下的关键词
junit4 - MethodSorters
那么我们现在要做的就是查询junit5该怎么进行测试排序
百度这方面资料比较少,幸运的是Stackoverflow一下就找到了,应该使用@TestMethodOrder
Stackoverflow - Junit's @Testmethodorder annotation not working
调整之后排序有效
package com.task;
import java.util.LinkedList;
import java.util.List;
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;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.task.entity.Platform;
import com.task.repository.PlatformRepository;
import junit.framework.TestCase;
@DataJpaTest
@TestMethodOrder(OrderAnnotation.class)
class PlatformRepositoryTest extends TestCase {
@Autowired
private PlatformRepository platformRepository;
//Hope to run in order of creation, selection, and deletion
@Test
@Order(1)
public void createTest() {
System.out.println("============ Enter PlatformRepository createTest ===========");
System.out.println("============ Exit PlatformRepository createTest ===========");
}
@Test
@Order(2)
public void selectTest() {
System.out.println("============ Enter PlatformRepository selectTest ===========");
System.out.println("============ Exit PlatformRepository selectTest ===========");
}
@Test
@Order(3)
public void deleteTest() {
System.out.println("============ Enter PlatformRepository deleteTest ===========");
System.out.println("============ Exit PlatformRepository deleteTest ===========");
}
}
结果
============ Enter PlatformRepository createTest ===========
============ Exit PlatformRepository createTest ===========
============ Enter PlatformRepository selectTest ===========
============ Exit PlatformRepository selectTest ===========
============ Enter PlatformRepository deleteTest ===========
============ Exit PlatformRepository deleteTest ===========
网友评论