为解决测试用例中既有org.junit.Test也有org.junit.jupiter.api.Test情况
在这种情况下如果没有添加对应引擎,则可能只能跑org.junit.jupiter.api.Test的用例,或者什么都没有执行.
因此需要添加对应引擎依赖以正常跑测试用例.
注意: 在编辑依赖前请先检查依赖,如果已经依赖或间接依赖了不合适的版本,需要先排除(如groovy-all会自带测试依赖).
junit4
maven依赖中添加junit-vintage-engine即可
<dependencies>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit.vintage.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- ...other -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.version}</version>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
</plugins>
</build>
然后就可以正常用mvn -Dtest=local.my.t.Test4Test test命令执行如下java用例:
package local.my.t;
public class Test4Test {
@org.junit.jupiter.api.Test
public void t1(){
System.err.println("test t1");
}
@org.junit.Test
public void t2(){
System.err.println("test t2");
}
}
用scala编写的用例也可以正常用mvn -Dtest=Test5Test test命令执行,注意Before和BeforeEach只能对相应版本的Test生效
package local.my.t
class Test5Test {
@org.junit.Before
def before1() {
Console.err.println("test scala 5 junit before")
}
@org.junit.jupiter.api.BeforeEach
def before2() {
Console.err.println("test scala 5 junit jupiter")
}
@org.junit.Test
def test1(): Unit = {
Console.err.println("test scala 5 junit")
}
@org.junit.jupiter.api.Test
def test2(): Unit = {
Console.err.println("test scala 5 jupiter")
}
}
junit5+junit4
maven依赖中还需要加上junit-jupiter-engine的依赖(5.12及以上不一定行)
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit.vintage.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.version}</version>
</plugin>
</plugins>
</build>