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