运行maven测试

fkaflof6  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(341)

我正在学习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] ------------------------------------------------------------------------

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

项目目录结构
测试文件位于src/test/java中

iyr7buue

iyr7buue1#

两个原因:
测试类名与surefire配置中的regex不匹配。您已经明确指出类名需要匹配test
您正在从org.junit而不是org.junit.jupiter.api.test导入@test注解

相关问题