未在使用JUnit5和Spring-boot-starter-parent时执行任何测试。

huangapple go评论87阅读模式
英文:

No Tests Were Executed when using JUnit5 and Spring-boot-starter-parent

问题

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<!-- Piece to be disabled for tests to run -->
<!-- <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
<!-- </parent> -->

<groupId>com.hmhco</groupId>
<artifactId>crs-v2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>crs-v2</name>
<description>Content Recommendation Service V2</description>

<properties>
    <java.version>1.8</java.version>
    <junit.jupiter.version>5.6.2</junit.jupiter.version>
    <maven.surefire.version>3.0.0-M5</maven.surefire.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven.surefire.version}</version>
        </plugin>
    </plugins>
</build>
</project>
import org.junit.jupiter.api.Test;

public class TestingTest {

@Test
public void checking() {
    System.out.println("checking------------");
}
}

谢谢。

英文:

I have a very simple JUnit test class that I can not run unless I remove parent spring-boot-starter-parent from the pom, which wouldn't be a possibility for our production application. The error we get on is No Tests Were Executed and below is the mvp with the parent piece that whenever not commented out would block the tests. If I can get any guidance to know how to fix this please.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; 
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
     xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;

&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
 
&lt;!-- Piece to be disabled for tests to run --&gt;
&lt;parent&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
    &lt;version&gt;2.2.4.RELEASE&lt;/version&gt;
    &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
  &lt;/parent&gt;

&lt;groupId&gt;com.hmhco&lt;/groupId&gt;
&lt;artifactId&gt;crs-v2&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;name&gt;crs-v2&lt;/name&gt;
&lt;description&gt;Content Recommendation Service V2&lt;/description&gt;

&lt;properties&gt;
    &lt;java.version&gt;1.8&lt;/java.version&gt;
    &lt;junit.jupiter.version&gt;5.6.2&lt;/junit.jupiter.version&gt;
    &lt;maven.surefire.version&gt;3.0.0-M5&lt;/maven.surefire.version&gt;
&lt;/properties&gt;

&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
        &lt;artifactId&gt;junit-jupiter-api&lt;/artifactId&gt;
        &lt;version&gt;${junit.jupiter.version}&lt;/version&gt;
        &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
        &lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
        &lt;version&gt;${junit.jupiter.version}&lt;/version&gt;
        &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;

&lt;build&gt;
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
        &lt;/plugin&gt;
        &lt;plugin&gt;
            &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
            &lt;version&gt;${maven.surefire.version}&lt;/version&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/build&gt;

</project>

import org.junit.jupiter.api.Test;

public class TestingTest {

@Test
public void checking() {
    System.out.println(&quot;checking------------&quot;);
}
}

Thank you.

答案1

得分: 1

问题与您尝试使用不同版本的JUnit Jupiter有关。spring-boot-parent(2.2.4.RELEASE)预定义的版本是5.5.2。最简单的解决方案是删除junit-jupiter部分的版本(版本标签),并使用从spring-boot-parent继承的版本。我能给出的最好建议是从新版本的Spring Boot(最新版本为2.3.3.RELEASE)开始,该版本使用了更新的JUnit版本。

如果您无法这样做,您必须使用junit-bom文件,如下所示:

<!-- language:language-xml -->
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.junit</groupId>
      <artifactId>junit-bom</artifactId>
      <version>5.6.2</version>
      <scope>import</scope>
      <type>pom</type>
    </dependency>
  </dependencies>
</dependencyManagement>

然后它应该正常工作。之后,您在pom.xml文件中不再需要定义版本标签来使用依赖项。

英文:

The problem is related to that you trying to use a different version of junit jupiter. The version which is predefined by spring-boot-parent (2.2.4.RELEASE) is 5.5.2. The simplest solution is to remove the versions of junit-jupiter parts (version tag) and use the one which is inherited from spring-boot-parent. The best recommendation I can give is to start with a newer version of Spring Boot (2.3.3.RELEASE most recent one) which uses a more recent version.

If you can't go that way you have to use the junit-bom file instead like this:

<!-- language:language-xml -->

  &lt;dependencyManagement&gt;
    &lt;dependencies&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;org.junit&lt;/groupId&gt;
        &lt;artifactId&gt;junit-bom&lt;/artifactId&gt;
        &lt;version&gt;5.6.2&lt;/version&gt;
        &lt;scope&gt;import&lt;/scope&gt;
        &lt;type&gt;pom&lt;/type&gt;
      &lt;/dependency&gt;

Then it should work fine. Afterwards you have to use the dependencies without defining the version tag anymore in your pom.xml file.

答案2

得分: -1

将-DfailIfNoTests=false添加到mvn命令中似乎使其工作正常。

英文:

Adding -DfailIfNoTests=false to the mvn command seems to make it work.

huangapple
  • 本文由 发表于 2020年8月18日 17:44:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63465987.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定