Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当添加到DependencyManager中时,SpringBoot不会运行单元测试_Java_Spring Boot_Maven_Unit Testing_Junit5 - Fatal编程技术网

Java 当添加到DependencyManager中时,SpringBoot不会运行单元测试

Java 当添加到DependencyManager中时,SpringBoot不会运行单元测试,java,spring-boot,maven,unit-testing,junit5,Java,Spring Boot,Maven,Unit Testing,Junit5,我的项目应该是从一个自定义父级继承的,同时使用SpringBoot。 这方面的标准解决方案是使用上述章节 问题是,当一个Spring引导依赖项被添加到部分中时,maven看不到任何单元测试测试运行=0。我用spring boot依赖项和spring boot starter复制了这一点 复制步骤: 使用一个单元测试SampleTest和以下pom创建一个maven项目。 运行mvn测试。 查看SampleTest是否已运行并失败 取消对中的块的注释以导入Spring Boot 运行mvn测试 请

我的项目应该是从一个自定义父级继承的,同时使用SpringBoot。 这方面的标准解决方案是使用上述章节

问题是,当一个Spring引导依赖项被添加到部分中时,maven看不到任何单元测试测试运行=0。我用spring boot依赖项和spring boot starter复制了这一点

复制步骤:

使用一个单元测试SampleTest和以下pom创建一个maven项目。 运行mvn测试。 查看SampleTest是否已运行并失败 取消对中的块的注释以导入Spring Boot 运行mvn测试 请参阅没有运行任何测试,并且生成已成功。 我对旧版本的surefire也有类似的问题,它找不到类似方式的JUnit5测试。但是有效的pom表明,在这种情况下,插件的版本被正确设置为3.0.0-M4,并且没有被SpringBoot覆盖

您能帮我解决运行测试的问题并在这种情况下正确应用Spring Boot吗

src/test/java/org/example/SampleTest.java

pom.xml

以下示例没有父级:我刚刚复制了插件 并从中提取相关性以保持简洁

包括spring引导依赖项将更改junit jupiter引擎依赖项的可传递依赖项

mvn dependency:tree的输出将显示这一点

以前

之后

正如您所看到的,junit平台引擎和好友从1.6.0更改为1.5.2,API从5.6.0更改为5.5.2。由于这些JAR是不兼容的版本,您的测试将不再运行

要修复此问题,可以执行以下操作之一

使用显式版本添加所有附加的、可传递的依赖项 将junit bom也添加到Spring Boot bom之前的dependencyManagement部分,以强制更新版本 降级到JUnit 5.5.2以与Spring引导管理版本保持一致 包括spring引导依赖项将更改junit jupiter引擎依赖项的可传递依赖项

mvn dependency:tree的输出将显示这一点

以前

之后

正如您所看到的,junit平台引擎和好友从1.6.0更改为1.5.2,API从5.6.0更改为5.5.2。由于这些JAR是不兼容的版本,您的测试将不再运行

要修复此问题,可以执行以下操作之一

使用显式版本添加所有附加的、可传递的依赖项 将junit bom也添加到Spring Boot bom之前的dependencyManagement部分,以强制更新版本 降级到JUnit 5.5.2以与Spring引导管理版本保持一致
另一个解决方案是在spring引导之前为junit jupiter定义导入。这也解决了问题

  <dependencyManagement>
    <dependencies>    
      <dependency>
        <groupId>org.junit</groupId>
        <artifactId>junit-bom</artifactId>
        <version>5.6.0</version>
        <scope>import</scope>
        <type>pom</type>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.2.5.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

另一个解决方案是在spring引导之前为junit jupiter定义导入。这也解决了问题

  <dependencyManagement>
    <dependencies>    
      <dependency>
        <groupId>org.junit</groupId>
        <artifactId>junit-bom</artifactId>
        <version>5.6.0</version>
        <scope>import</scope>
        <type>pom</type>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.2.5.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
如果您使用的是SpringBoot2.x,那么默认情况下它支持JUnit5,并且在构建项目或执行mvn测试时,不会读取用JUnit4编写的所有测试用例。 要修复此问题,请使用pom.xml中的以下代码段:

org.apache.maven.plugins maven surefire插件 2.8 org.apache.maven.surefire surefire-junit4 2.8 注意:使用上述方法将跳过JUnit 5测试用例。

如果您使用的是Spring Boot 2.x,那么默认情况下它支持JUnit 5,并且在构建项目或执行mvn测试时不会读取用JUnit 4编写的所有测试用例。 要修复此问题,请使用pom.xml中的以下代码段:

org.apache.maven.plugins maven surefire插件 2.8 org.apache.maven.surefire surefire-junit4 2.8
注意:使用上述方法将跳过JUnit 5测试用例。

这应该是因为?向DependencyManager部分添加某些内容不会将依赖项添加到项目中。@M.Denium我不希望自动使用spring boot starter webflux或其他内容,因为在depsManagement中取消对该块的注释。我希望它不会破坏已经起作用的东西,而只是找到了如何重现问题的最短示例。实际上你的评论太简洁了,我不明白你的意思。你能详细说明一下吗?你知道project找不到测试的原因吗?我忽略了你已经拥有junit依赖项的事实,你不会是第一个也不是最后一个误解DependencyManager部分的人。DependencyManager导入Spring Boot管理的依赖项,并更改junit jupiter引擎的一些依赖项版本,因为它们可能是明确定义的。它们被降级到5.5.2,这可能是不兼容的。如果您使用传递词运行maven依赖性检查,您将看到您是否比较befopre和after?向DependencyManager添加内容
ent部分不会将依赖项添加到您的项目。@M.Denium我不希望自动使用spring boot starter webflux或其他东西,因为在depsManagement中取消了对块的注释。我希望它不会破坏已经起作用的东西,而只是找到了如何重现问题的最短示例。实际上你的评论太简洁了,我不明白你的意思。你能详细说明一下吗?你知道project找不到测试的原因吗?我忽略了你已经拥有junit依赖项的事实,你不会是第一个也不是最后一个误解DependencyManager部分的人。DependencyManager导入Spring Boot管理的依赖项,并更改junit jupiter引擎的一些依赖项版本,因为它们可能是明确定义的。它们被降级到5.5.2,这可能是不兼容的。如果你对过渡词进行maven依赖性检查,你会发现你是否比较了befopre和after。伙计,你太棒了。我知道spring boot覆盖了一些东西,但我怀疑插件,并没有注意到jupiter混乱。@khmarbaise他不是这么说的,他认为spring boot依赖性覆盖了插件版本。伙计,你太棒了。我知道spring boot覆盖了一些东西,但我怀疑插件,并没有注意到jupiter混乱。@khmarbaise他不是这么说的,他认为spring boot依赖性覆盖了插件版本。我也有同样的问题。当我遵循@shanky442的建议时,问题解决了。谢谢我也有同样的问题。当我遵循@shanky442的建议时,问题解决了。谢谢
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ spring-boot-import ---
[INFO] org.example:spring-boot-import:jar:1.0
[INFO] \- org.junit.jupiter:junit-jupiter-engine:jar:5.6.0:test
[INFO]    +- org.apiguardian:apiguardian-api:jar:1.1.0:test
[INFO]    +- org.junit.platform:junit-platform-engine:jar:1.6.0:test
[INFO]    |  +- org.opentest4j:opentest4j:jar:1.2.0:test
[INFO]    |  \- org.junit.platform:junit-platform-commons:jar:1.6.0:test
[INFO]    \- org.junit.jupiter:junit-jupiter-api:jar:5.6.0:test
[INFO] org.example:spring-boot-import:jar:1.0
[INFO] \- org.junit.jupiter:junit-jupiter-engine:jar:5.6.0:test
[INFO]    +- org.apiguardian:apiguardian-api:jar:1.1.0:test
[INFO]    +- org.junit.platform:junit-platform-engine:jar:1.5.2:test
[INFO]    |  +- org.opentest4j:opentest4j:jar:1.2.0:test
[INFO]    |  \- org.junit.platform:junit-platform-commons:jar:1.5.2:test
[INFO]    \- org.junit.jupiter:junit-jupiter-api:jar:5.5.2:test
  <dependencyManagement>
    <dependencies>    
      <dependency>
        <groupId>org.junit</groupId>
        <artifactId>junit-bom</artifactId>
        <version>5.6.0</version>
        <scope>import</scope>
        <type>pom</type>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.2.5.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>