Sprint Boot Junit5测试无法运行



我在一个为Junit 4编写的spring-boot应用程序中测试了对数据库的访问。我把这个测试改为Junit 5。在我的pom.xml中添加了以下内容。

<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.5.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>funnel-backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>funnel-backend</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<junit.jupiter.version>5.5.2</junit.jupiter.version>
<junit.platform.version>1.5.2</junit.platform.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</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.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

测试本身是相当简单的

@SpringBootTest
@ExtendWith(SpringExtension.class)
public class UserDataTest {
@Autowired
UserDataRepository repository;
@Test
void testAddAnswers() {
UserData data = new UserFunnelData(1L);
data.addData("SomeData",true);
repository.save(data);
Optional<UserData>  lookup = repository.findById(1L);
assertTrue(lookup.isPresent());
assertEquals("SomeData", lookup.get().getData());
repository.deleteById(1L);//Cleaning up.
}
}

当我尝试运行这个intellij时,我在控制台上看不到任何东西。只是弹出的消息";未能运行";。我知道这没什么好说的。如果有任何关于如何应对这一问题的建议或建议,我将不胜感激。

您有spring-boot 2.5,它已经包含了jupiter。pom文件中对junit的排除对于旧版本是必需的。

大多数开发人员使用spring-boot-starter测试"starter",它导入了spring-boot测试模块以及JUnit Jupiter、AssertJ、Hamcrest和许多其他有用的库。

如果您有使用JUnit4的测试,那么可以使用JUnit5的老式引擎来运行它们。要使用复古引擎,请添加对junit复古引擎的依赖,如以下示例所示:

<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>

最新更新