【问题标题】:Spring Boot maven unit tests not being executedSpring Boot maven单元测试未执行
【发布时间】:2018-12-18 14:15:20
【问题描述】:

我有一个 Spring Boot 项目。我已经在 .../src/test/java/... 目录中编写了单元测试。我所有的单元测试都是 *Test.java 的形式。当我运行“mvn test”时,我得到以下输出:

[INFO]  
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ frm-api ---  
[INFO] Changes detected - recompiling the module!  
[INFO] Compiling 6 source files to /Users/JoelRives/Projects/fasor/fasor-service/fatality-review/target/test-classes  
[INFO]  
[INFO] --- maven-surefire-plugin:2.22.0:test (default-test) @ frm-api ---  
[INFO]  
[INFO] -------------------------------------------------------  
[INFO]  T E S T S  
[INFO] -------------------------------------------------------  
[INFO]   
[INFO] Results:  
[INFO]  
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0  
[INFO]  

显然,maven 在编译单元测试时会看到它们。但是,如您所见,它不会运行它们。

我的 pom 中有以下相关的 maven 依赖项:

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

  <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <scope>test</scope>
      <version>1.4.194</version>
  </dependency>

我的 pom 还包括以下插件:

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.0</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.3.0-M1</version>
                </dependency>
                <dependency>
                <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.2.0-M1</version>
                </dependency>
            </dependencies>
        </plugin>

非常感谢您对此的任何帮助。

我没有展示我的整个 POM。这是 Junit 的东西:

<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
  </dependency>

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

  <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <scope>test</scope>
      <version>1.4.194</version>
  </dependency>

【问题讨论】:

  • 你的测试类有 JUnitRunner 吗?像'@RunWith(MyTestRunner.class)'
  • 您正在使用 Unit 4,但已为 junit5 配置了surefire...
  • 我的surefire插件的哪一部分为junit5配置了它?

标签: java spring maven unit-testing spring-boot


【解决方案1】:

对我来说,我在我的pom.xml 中写了这个并且我使用的是 JUnit4

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

一旦我删除了exclusions,它就开始执行了。

【讨论】:

  • 我也有同样的情况。一旦我删除了排除的部分,它就会接受所有的测试。
  • 感谢@Ganesh 上述解决方案对我有用。我使用的是 Junit 4,即使在我添加了surefire插件之后,测试也没有被选中。删除上述排除项就成功了!
【解决方案2】:

看起来如果您使用的是 Junit4 和 surefire 插件版本 2.22 及更高版本,则测试不会被选中。我遇到了类似的问题,使用 surefire V2.21.0 似乎可以工作。

下面是我的万无一失的配置

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

【讨论】:

    【解决方案3】:

    使用最近的 Surefire,您可以将其配置为使用 JUnit 4:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M5</version>
        <dependencies>
          <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit47</artifactId>
            <version>3.0.0-M5</version>
          </dependency>
        </dependencies>
      </plugin>
    

    【讨论】:

      【解决方案4】:

      注意,这是部分答案。我可以通过从 POM 中删除 surefire 插件来运行单元测试。过去我从来没有遇到过万能的问题。哦...,至少测试正在运行——尽管它们可能已损坏:-) 我稍后会调查万无一失的问题。感谢您的建议。

      【讨论】:

        【解决方案5】:

        尝试更新surefire-plugin 以明确选择测试类:

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

        你也可以看看this,可能会重复!!

        【讨论】:

        • 没错,我想知道如果JUnit依赖本身丢失并且正在使用,代码会编译吗?
        • 这对我有帮助。默认情况下,插件采用以下名称格式的所有类:*Test.java,但会跳过所有在测试后名称中包含任何内容的测试(例如 IntegrationTest_4XX.java)。
        猜你喜欢
        • 2018-11-29
        • 1970-01-01
        • 2016-05-28
        • 2017-02-20
        • 1970-01-01
        • 2014-10-25
        • 2015-12-20
        • 1970-01-01
        相关资源
        最近更新 更多