运行 Maven 测试

Running Maven Tests

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

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

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] ------------------------------------------------------------------------

为什么测试没有运行?当我使用 运行 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

两个原因:

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

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