【问题标题】:Sprint Boot Junit5 Test fails to runSpring Boot Junit 5 测试无法运行
【发布时间】:2021-09-05 16:08:43
【问题描述】:

我在 Spring Boot 应用程序中测试了对 db 的访问,该应用程序是为 Junit 4 编写的。 我将此测试更改为 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 时,我在控制台上看不到任何东西。只是弹出“运行失败”的消息。我知道这不是很多事情要做。我将不胜感激有关如何攻击此问题的任何指示或建议。

【问题讨论】:

  • 您可以删除@ExtendWith(SpringExtension.class),这已经是@SpringBootTest 的一部分。如果你只是想测试一个存储库,你可以使用@DataJpaTest 而不是@SpringBootTest。优点是您无需手动清理此类测试的数据。您可以尝试从命令行运行mvn test 看看是否有效吗?

标签: spring-boot spring-data-jpa junit5


【解决方案1】:

你有 spring boot 2.5,它已经包含 jupiter。旧版本需要您在 pom 文件中排除的 junit。

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

如果您有使用 JUnit 4 的测试,则可以使用 JUnit 5 的老式引擎来运行它们。要使用vintage引擎,需要添加对junit-vintage-engine的依赖,如下例所示:

<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>

source

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-27
    • 2020-03-20
    • 1970-01-01
    • 2020-07-13
    • 2019-01-30
    • 2018-01-04
    • 2018-11-17
    • 2018-05-11
    相关资源
    最近更新 更多