【问题标题】:Running Maven Tests运行 Maven 测试
【发布时间】:2020-12-20 21:24:29
【问题描述】:

我正在学习Java,部分课件是Maven。

我创建了一个简单的 Maven 项目。在测试类中我添加了以下代码:

package maventest2;

import org.junit.Test;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;

public class firstclass {
    
@Tag("FirstTests")
@DisplayName("This is the first group test I working on")
@Test

public void someTest() {
    System.out.println("This in some test running");
}
}

当我运行 mvn test 命令时,我得到以下信息:

Microsoft Windows [Version 10.0.18363.1256]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\Users\pawan\eclipse-workspace\maventest2>mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.taskone:maventest2 >-----------------------
[INFO] Building maventest2 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maventest2 ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maventest2 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maventest2 ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maventest2 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M3:test (default-test) @ maventest2 ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.139 s
[INFO] Finished at: 2020-12-19T23:32:34-05:00
[INFO] ------------------------------------------------------------------------

为什么没有运行测试?当我使用 Run As -> JUnit Test 选项时。它工作正常,我得到了通过的结果。

请指教。

pom 文件:

<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>
  <groupId>com.taskone</groupId>
  <artifactId>maventest2</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <build>
  <plugins>
   <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M3</version>
                <configuration>
                    <includes>
                        <include>%regex[.*Test.*]</include>
                    </includes>
                </configuration>
            </plugin>
  
    
  </plugins>
    
  
  </build>
  
  <dependencies>
  <dependency>
  <groupId>junit</groupId>     <!-- NOT org.junit here -->
  <artifactId>junit-dep</artifactId>
  <version>4.8.2</version>
  <scope>test</scope>
 </dependency>
 
 <dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.7.0</version>
    <scope>test</scope>
</dependency>
 
 
  </dependencies>
</project>

Directory structure of project

测试文件在 src/test/java

【问题讨论】:

  • 你的测试类在哪个目录下?并将您的 pom.xml 添加到您的问题中。
  • 根据你的目录结构图,测试类在src/test/java(应该是这个位置,而不是src/test/resources。你能澄清一下吗?
  • Java 中的第一个类以大写字母开头 第二个测试(单元测试)应命名为 ClassTest.java ...此外,我强烈建议不要将 JUnit 4 与 JUnit Jupiter 混合使用...今天简单使用 JUnit JUpiter(又名 JUnit 5)...并删除 junit-dep ?使用不需要也没有用的正则表达式删除配置...
  • @khmarbaise 谢谢你的提示。知道了。对这一切还是陌生的,我迷路了。

标签: java maven junit


【解决方案1】:

两个原因:

  1. 您的测试类名称与您在surefire 配置中的正则表达式不匹配。您已明确表示类名需要匹配 *Test*

  2. 您是从 org.junit 导入 @Test 注解,而不是 org.junit.jupiter.api.Test

【讨论】:

  • 现在工作。非常感谢@tgdavies。
猜你喜欢
  • 2012-04-06
  • 2013-08-23
  • 1970-01-01
  • 2014-08-24
  • 2012-06-19
  • 2021-10-19
  • 2017-01-22
  • 2014-09-28
  • 2020-12-19
相关资源
最近更新 更多