[英]Unit testing java maven
我有一个 maven 项目,如下所示:
如果我在终端运行:
mvn test
它将构建应用程序并运行 3 个测试:
SocialMultiplicationApplicationTests
MultiplicationServiceTest
RandomGeneratorServiceTest
但不是RandomGeneratorServiceImplTest
。
如果我尝试显式运行此 class:
mvn test -Dtest=RandomGeneratorServiceImplTest
我得到:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test)
on project social-multiplication: No tests were executed! (Set -DfailIfNoTests=false to ignore this error.) -> [Help 1]
这是我的 pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>microservices.book</groupId>
<artifactId>social-multiplication</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>social-multiplication</name>
<description>Social Multiplication App</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
这是不会运行的测试 class:
package microservices.book.multiplication.service;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Before;
import org.junit.Test;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class RandomGeneratorServiceImplTest {
private RandomGeneratorServiceImpl randomGeneratorServiceImp;
@Before
public void setUp(){
randomGeneratorServiceImp = new RandomGeneratorServiceImpl();
}
@Test
public void generateRandomFactorIsBetweenExpectedLimits() {
List<Integer> randomFactors = IntStream.range(0,1000)
.map(i -> randomGeneratorServiceImp.generateRandomFactor())
.boxed()
.collect(Collectors.toList());
for(Integer i: randomFactors){
assertThat(i).isIn(
IntStream.range(11, 100).
boxed().collect(Collectors.toList()));
}
}
}
编辑:解决方案
问题确实是junit4和junit5之间的冲突。
我选择将测试移至 junit5,因此我将@Before
替换为@BeforeAll
并在RandomGeneratorServiceImplTest
class 定义之上添加了@TestInstance(Lifecycle.PER_CLASS)
。
我发现有用的几个链接:
原因在于混合了 JUnit 5 和 JUnit 4。maven maven-surefire-plugin
只选择一种策略来执行测试,在这种情况下偏向 JUnit 5。
<!-- <dependency>-->
<!-- <groupId>junit</groupId>-->
<!-- <artifactId>junit</artifactId>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
...
//import org.junit.Before;
//import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
...
public class RandomGeneratorServiceImplTest {
...
// @Before
@BeforeEach
public void setUp(){
...
}
// @Test can stay as is, only import needs to be updated
@Test
public void generateRandomFactorIsBetweenExpectedLimits() {
...
}
}
尝试将此插件添加到 pom.xml 中的构建部分:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.