繁体   English   中英

Maven 未运行 Spring 引导测试

[英]Maven not running Spring Boot tests

我有一个 rest 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 class 的一部分:

@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 class 的顶部,我有这些注释:

@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 插件,但它似乎正在寻找它。

您命名为TestController的类中的代码不是控制器,而是测试,但约定说它是控制器(可能用于测试)。 默认情况下,Surefire 将寻找匹配*Test的测试; 将类重命名为ControllerTest

使用下面的 Maven 罐子。 它解决了我的问题。

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

即使不推荐(因为不是标准),您也可以配置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 之前添加了通配符

尝试检查您是否为 @Test 注释使用了正确的包。 在 Spring Boot 2.3.6 中是org.junit.jupiter.api.Test

发生这种情况的另一个原因是在你的 pom.xml 中声明了另一个万能插件。 就我而言,我将一个应用程序迁移到 spring boot 并将其留在 pom.xml 中。

    <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.xml 中删除此部分后执行 Spring Boot 测试。

由于这个问题首先出现在搜索引擎上,我认为这对寻找SpringBoot 2.4及以后版本的有线行为解决方案的人们会有所帮助。

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

原因- SpringBoot 已迁移到junit5 ,这就是 maven 忽略junit4测试用例的原因。 要查看详细的测试依赖关系,请探索 pom 的spring-boot-starter-test

解决方案 1 - 在junit5中使用类级别注释@SpringBootTest并遵循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>

如果以上都不适合您,并且您正在使用带有适当注释的 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>

参考这里的例子

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

使用 Spring boot 2.5.0 junit-vintage-engine jar 未添加,因此任何 org.junit.Test 都不会运行。

只有 org.junit.jupiter.api.Test 似乎在运行。

添加这个 jar(junit-vintage-engine)后,我所有的旧测试(org.junit.Test)都运行得很好。

可能会发生此问题,因为您的测试类不是公开的。

这适用于我将 springboot 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>

这里的关键是了解Spring使用的测试引擎。

就我而言,我使用@RunWith(SpringRunner.class)编写了 API 测试,该测试位于 JUnit4 下,与junit-vintage-engine一起运行。

但是单元测试是用 JUnit Jupiter 编码的,它在 JUnit5 下,它与junit-jupiter-engine一起运行。

SpringBoot 的默认引擎是junit-jupiter-engine 所以我们必须告诉 Spring 我们也想使用junit-vintage-engine

我们可以简单地在 pom.xml 中添加依赖项:

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

或者,如果我们想使用 maven-surefire,我们可以在插件中添加依赖:

<plugin>
  <groupId>org.apache.maven.plugins</groupId> 
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.0.0-M7</version>
  <dependencies>
    <dependency>
      <groupId>org.junit.vintage</groupId>
      <artifactId>junit-vintage-engine</artifactId>
      <version>5.7.0</version>
    </dependency>
  </dependencies>
</plugin>

如果您在使用 spring 启动 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>

对于仍然有问题的其他人。 我个人通过删除解决了这个问题:

<packaging>pom</packaging>

来自pom.xml文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM