注解SpringBootTest忽略我的测试

问题描述 投票:0回答:1

假设我有一个Spring Boot项目,基于SpringBoot,包括

pom.xml
中的以下依赖:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

SpringBoot 有版本

2.7.18
,surefire 有版本
2.22.2
和 Junit
5.8.2
。我的maven版本是
3.9.6

现在,我有一些测试类,遵循约定,文件名以

Tests.java
结尾。 对于某些测试,我需要特定的属性,这些属性通常由文件
application.properties
设置。我不想使用更多文件,而是想使用
@SpringBootTest-Annotation
,如下所示:

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
//Other imports

@SpringBootTest(properties={"testproperty=value"})
public class ExampleTests {
    @Test
    public void ExampleValidTest() {
        // Do some testing
    }
}

如果我现在运行

mvn clean package
,这将找到其他测试类中给出的所有其他测试,但不会找到注释标记的此测试。如果我删除此注释,该测试将被执行,但失败(基于错误的属性值,这很明显)。

有人可以帮忙吗?

java spring-boot
1个回答
0
投票

尝试将依赖项

spring-boot-test
替换为
spring-boot-starter-test
:

这个

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

这应该可以解决你的问题

© www.soinside.com 2019 - 2024. All rights reserved.