Maven无法运行Spring Boot测试

54

我有一个Spring Boot REST API需要测试。在Eclipse中手动运行测试(不使用maven,而是将应用程序作为JUnit测试运行)可以正常运行并显示结果,但是mvn test不起作用,下面你将发现问题所在。

这是我的POM文件:

http://maven.apache.org/xsd/maven-4.0.0.xsd">4.0.0

<groupId>org.demo</groupId>
<artifactId>rest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>UserRegistrationServices</name>
<description>RESTful API</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>

    <!-- Junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- Spring dependencies -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <!-- To deploy to external servlet container -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- For Spring Boot testing -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.jayway.restassured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>2.4.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.jayway.restassured</groupId>
        <artifactId>json-schema-validator</artifactId>
        <version>2.4.1</version>
        <scope>test</scope>
    </dependency>

    <!-- For returning objects as JSON -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.5.4</version><!--$NO-MVN-MAN-VER$ -->
    </dependency>
    <dependency>
        <groupId>com.fasterxml</groupId>
        <artifactId>jackson-xml-databind</artifactId>
        <version>0.6.2</version>
    </dependency>

    <!-- To decode Base64 data -->
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.10</version>
    </dependency>
</dependencies>

<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <url>http://repo.spring.io/snapshot</url>
    </pluginRepository>
</pluginRepositories>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
这是运行mvn test的结果:
[INFO] Scanning for projects...
[INFO] 
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building UserRegistrationServices 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ rest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ rest ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 9 source files to C:\Users\pmandayam\git\UserRegistrationServices\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ rest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\pmandayam\git\UserRegistrationServices\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ rest ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\pmandayam\git\UserRegistrationServices\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.17:test (default-test) @ rest ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.768 s
[INFO] Finished at: 2015-07-28T12:07:41-05:00
[INFO] Final Memory: 24M/212M
[INFO] ------------------------------------------------------------------------

以下是我在 src/test/java 中的 TestController.java 类的一个片段:

@Test
    public void f_findByUsername() {
        // Finding user with username 'user1username'

        given().auth().basic("User1username", "Testpassword").when().get(
                "http://localhost:8080/users/get/ByUsername?username=User1username")
                .then().assertThat().body("username", is("User1username"));
    }

在TestController类的顶部,我有以下注释:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
/* Tells the embedded Tomcat server to start on a random, open port */
@IntegrationTest("server.port:0")
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestController {....}

我不确定出了什么问题。我没有surefire插件,但它似乎正在寻找它。

14个回答

56

你命名为TestController的类中的代码并不是控制器,而是测试代码,但是惯例上说它是一个控制器(可能用于测试)。默认情况下,Surefire将寻找与*Test匹配的测试内容;请将该类重命名为ControllerTest


关于测试驱动编程的命名方案,这是一个非常重要的声明! - B--rian
1
可以工作了!我遇到了同样的问题,在将名称重命名为"...Test"之后,Maven开始执行我的测试。 - Ev.Rei.

56

使用下面的 Maven Jar 包。它修复了我的问题。

<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <scope>test</scope>
</dependency>

14
如果你正在使用最新的Spring Boot版本和JUnit 4,则需要进行此操作。请参见:https://www.baeldung.com/spring-boot-testing#1-junit-4 - AlexO
4
你救了我的一天。这个依赖项是最新的Spring Boot应用程序所必需的。 - sampathlk
如果您正在使用org.junit.Test,则此解决方案可行。 - Adam Mudianto
妈妈呀!不知道为什么我的测试覆盖率变成了0... - PragmaticProgrammer

26

请检查您是否使用了正确的包来使用@Test注解。在Spring Boot 2.3.6中,正确的包是org.junit.jupiter.api.Test


通过在测试方法注释中使用此类进行修复:+1。 - Alex

17

尽管这并不被建议(因为不是标准的做法),但是您也可以按照以下方式配置Maven Surefire插件

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
            <includes>
                <include>**/*Test*.java</include>
            </includes>
        </configuration>
    </plugin>
</plugins>

编辑:在 /Test*.java 前添加通配符


13

由于这个问题在搜索引擎上排名第一,我认为对于寻找解决方案的人来说会有所帮助,尤其是针对从 2.4 版本开始的SpringBoot 的奇怪行为。

问题 - 当你将SpringBoot项目从旧版本迁移到新版本(如 2.5.x)时,现有的junit测试用例会被maven忽略。

原因 - SpringBoot已经升级到junit5,因此maven正在忽略junit4测试用例。要查看详细的测试依赖项,请探索spring-boot-starter-test的POM文件。

解决方案1 - 或者您可以使用类级注释@SpringBootTest编写junit5测试用例,并遵循Junit5规则。

解决方案2 - 或者您可以在项目中使用junit-vintage依赖项来同时运行junit4junit5测试用例。

<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>5.6.3</version>
    <scope>test</scope>
</dependency>

6

这种情况发生的另一个原因可能是您在pom中声明了另一个surefire插件。在我的情况下,我将一个应用程序迁移到了Spring Boot,并在pom文件中留下了这个插件。

    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <testFailureIgnore>false</testFailureIgnore>
                <includes>
                    <include>**/*Test*.java</include>
                </includes>
            </configuration>
            <executions>
                <execution>
                    <phase>clean</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

如果从pom文件中删除此部分后,Spring Boot测试将被执行。


4

如果以上方法都不适用于您,并且您正在使用带有适当注释的junit 5,则问题可能出在Maven Surefire和Failsafe插件上。

这是因为JUnit Surefire提供程序与Surefire 2.22.0插件版本中的JUnit支持之间存在冲突。


更新您的POM以要求使用Maven Surefire和Failsafe插件的2.22.0版本。

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

请参考此处的示例。


0

这对于我的问题有效,可以将Spring Boot 2.3.9更新到2.4.3

<dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <version>5.6.3</version>
        <scope>test</scope>
</dependency>

0
如果你在使用Spring Boot 3时遇到了这个问题,我不得不将我的surefire插件版本升级到3.0.0-M8。
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M8</version>
</plugin>

0

添加 org.junit.jupiter.api.Test 对我有用


你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心找到有关如何编写良好答案的更多信息。 - Community
这并没有回答问题。一旦您拥有足够的声望,您将能够评论任何帖子;相反,提供不需要询问者澄清的答案。- 来自审核 - Dan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接