【问题标题】:JUnit 5 tests are not firing and tests aren't showing in test runnerJUnit 5 测试未触发且测试未显示在测试运行器中
【发布时间】:2020-04-26 17:49:45
【问题描述】:

我无法让 JUnit 5 测试在 VSCODE 上运行。我相信我的环境设置正确。 junit5-samples 的克隆 | junit5-jupiter-starter-maven 按预期构建和运行。我的项目没有。

我的项目在 maven 下构建,但没有运行任何测试。所以这可能是 Maven 或项目配置问题,与 VSCODE 无关。

我花了几个小时对各种想法进行了修改,但无济于事。有些不同,可能是一个错误,也许额外的一双眼睛可以找到它。非常感谢您提供的任何帮助。

这里是一些项目信息

'mvn clean package' 运行没有错误,但没有运行测试。 CodeLens 也无法正常工作。

运行测试|调试测试 丢失。测试运行程序中没有显示任何测试。

这是我的 POM 文件,其中包含一些编辑:

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.redacted</groupId>
  <artifactId>redacted</artifactId>
  <packaging>jar</packaging>
  <version>0.1.1</version>
  <name>redacted</name>

  <properties>
    <skipTests>false</skipTests>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
    <junit.jupiter.version>5.6.2</junit.jupiter.version>
  </properties>

  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>${junit.jupiter.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>${maven.compiler.source}</source>
          <target>${maven.compiler.target}</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.2</version>
        <configuration>
          <!-- <skipTests>${skipTests}</skipTests> -->
        </configuration>
      </plugin>
      <!-- <plugin>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.22.2</version>
      </plugin> -->
      <plugin>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-codegen-plugin</artifactId>
        <version>3.3.6</version>
        <executions>
          <execution>
            <id>generate-sources</id>
            <phase>generate-sources</phase>
            <configuration>
              <sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
              <wsdlOptions>
                <wsdlOption>
                  <wsdl>${basedir}/WSDL/redacted.wsdl</wsdl>
                </wsdlOption>
              </wsdlOptions>
            </configuration>
            <goals>
              <goal>wsdl2java</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

这是 mvn 命令的输出:

[INFO] 
[INFO] ------------------------< com.redacted:redactedClient >-------------------------
[INFO] Building redactedClient 0.1.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ redactedClient ---
[INFO] Deleting D:\repos\redacted\redactedClient\target
[INFO] 
[INFO] --- cxf-codegen-plugin:3.3.6:wsdl2java (generate-sources) @ redactedClient ---
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ redactedClient ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\repos\redacted\redactedClient\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ redactedClient ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 6 source files to D:\repos\redacted\redactedClient\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ redactedClient ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\repos\redacted\redactedClient\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ redactedClient ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\repos\redacted\redactedClient\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ redactedClient ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  8.444 s
[INFO] Finished at: 2020-04-26T09:55:55-07:00
[INFO] ------------------------------------------------------------------------

【问题讨论】:

  • 你应该添加junit-jupiter-engine作为依赖而不是junit-jupiter。 Surefire 2.22.X 将由此触发。在surefire 3.X 中,junit-jupier-api 正在工作
  • 我想为什么你的测试方法 GetModList() 返回的东西通常测试方法有 void ...你可以从测试方法中删除 public ,也可以从测试类本身中删除。
  • 我会尝试以上两种方法。我已经确定目标文件夹不包含 AppTest.class 类文件。那么可能没有选择 AppTest.java 进行编译?
  • @khmarbaise 不高兴切换到 void 和公开。由于某种原因,Maven 没有编译 AppTest.java 。目标文件夹中的文件名搜索显示未引用“AppTest”
  • @khmarbaise 请发表您关于 void 和 public 建议答案的第二条评论。

标签: java maven visual-studio-code junit5


【解决方案1】:

测试方法的返回类型应为void,而您的测试方法GetModList() 的返回类型为Boolean

在 JUnit Jupiter 中,您可以从测试类中删除 public,也可以从所有测试方法中删除。

【讨论】:

  • 快速说明:这花了我一段时间来整理,因为 ${basedir}src/test/java 中的这个 POM 条目也会导致相同的症状.我在发布问题和尝试@khmarbaise 的初步建议之间添加了它。那里的文件夹确实存在,所以我不确定它为什么会导致问题。对源代码做同样的事情就可以了。 ${basedir}/src/main/java
  • 您应该删除 &lt;sourceDirectory&gt;..&lt;/sourceDirectory&gt;&lt;testSourceDirectory&gt;..&lt;/testSourceDirectory&gt; 配置,因为它违反了配置范例的约定..
  • 目录覆盖不好。不幸的是,这是由于需要将 JUnit 添加到当前使用命令行测试的许多项目中。这些项目使用/src/com/domain/project/type/subtypes,而不是标准。我一直在测试的是我们可以添加测试吗?重构会有多困难。到目前为止,答案并不好。我把整个周末都花在了工具上,总之,我给管理层的报告将是“脆弱的”。
猜你喜欢
  • 1970-01-01
  • 2020-03-25
  • 2020-12-31
  • 1970-01-01
  • 2012-06-02
  • 2022-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多