【问题标题】:Maven not running Spring Boot testsMaven 没有运行 Spring Boot 测试
【发布时间】:2015-10-19 08:04:28
【问题描述】:

我有一个想要测试的剩余 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 {....}

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

【问题讨论】:

    标签: java spring maven unit-testing junit


    【解决方案1】:

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

    【讨论】:

    • 关于测试驱动编程命名方案的重要声明,确实!
    • 有效!我遇到了同样的问题,重命名为“...Test”后,Maven 开始执行我的测试。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      这可能发生的另一个原因是在你的 pom.xml 中声明了另一个万能插件。就我而言,我将一个应用程序迁移到 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】:

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

        <packaging>pom</packaging>
        

        来自 pom.xml 文件。

        【讨论】:

          【解决方案5】:

          使用下面的 maven jar。它解决了我的问题。

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

          【讨论】:

          • 如果您使用 JUnit 4 和最新的 Spring Boot 版本,这是必要的,请参阅:baeldung.com/spring-boot-testing#1-junit-4
          • 你拯救了我的一天。最新的 Spring Boot 应用程序需要此依赖项。
          • 如果您使用的是org.junit.Test,则此解决方案有效
          【解决方案6】:

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

          【讨论】:

            【解决方案7】:

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

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

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

            【讨论】:

              【解决方案8】:

              这个问题可能是因为你的测试类不是公开的。

              【讨论】:

                猜你喜欢
                • 2014-10-25
                • 1970-01-01
                • 1970-01-01
                • 2014-12-12
                • 2021-12-16
                • 1970-01-01
                • 2020-12-19
                • 2021-06-19
                • 1970-01-01
                相关资源
                最近更新 更多