【问题标题】:How to test methods in a java project using a maven junit5 framework project in eclipse如何在eclipse中使用maven junit5框架项目测试java项目中的方法
【发布时间】:2020-06-19 18:29:09
【问题描述】:

我创建了一个新的 maven junit5 框架项目来测试现有的 java 项目。我在新创建的 maven junit5 框架项目的构建路径中添加了 java 项目。我右键单击要为其添加 junit 测试用例的方法并选择新的 junit 测试用例并将源文件夹更改为新的 maven junit5 框架项目 src 目录,并将其余选项保留为默认值。创建了 junit 测试并将测试作为单元测试运行,没有任何问题(下面的屏幕截图)。使用 maven 运行相同的测试得到以下错误。我在 pom(below) 中添加了 surefire 插件,但仍然出现以下错误。使用日食。

-------------------------------------------------------------------------------
   Test set: com.build.VersionInfoTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.003 s <<< FAILURE! - in com.build.VersionInfoTest
com.build.VersionInfoTest  Time elapsed: 0.002 s  <<< ERROR!
java.lang.NoClassDefFoundError: Lcom/build/VersionInfo;
Caused by: java.lang.ClassNotFoundException: com.build.VersionInfo

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>UnitTesting</groupId>
<artifactId>com.unit.testing</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>com.unit.testing</name>
<description>Junit Tests</description>
<packaging>jar</packaging>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <junit.jupiter.version>5.5.2</junit.jupiter.version>
    <junit.platform.version>1.5.2</junit.platform.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-runner</artifactId>
        <version>${junit.platform.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
        </plugin>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
        </plugin>
    </plugins>
</build>
</project>

更新:我清理了 pom(下)但现在没有发现测试?当我用junit运行项目时,会发现测试吗?

  [INFO] Scanning for projects...
  [INFO] 
  [INFO] --------------< UnitTesting:com.unit.testing >---------------
  [INFO] Building com.unit.testing 1.0-SNAPSHOT
  [INFO] --------------------------------[ jar ]----------------------       -----------
  [INFO] 
  [INFO] --- maven-resources-plugin:2.6:resources (default-resources)      @ com.unit.testing ---
  [INFO] Using 'UTF-8' encoding to copy filtered resources.
  [INFO] Copying 0 resource
  [INFO] 
  [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @     com.unit.testing ---
  [INFO] Nothing to compile - all classes are up to date
  [INFO] 
  [INFO] --- maven-resources-plugin:2.6:testResources (default-    testResources) @ com.unit.testing ---
  [INFO] Using 'UTF-8' encoding to copy filtered resources.
  [INFO] Copying 0 resource
  [INFO] 
  [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ com.unit.testing ---
  [INFO] Nothing to compile - all classes are up to date
  [INFO] 
  [INFO] --- maven-surefire-plugin:3.0.0-M4:test (default-test) @   com.unit.testing ---
  [INFO] 
  [INFO] -------------------------------------------------------
  [INFO]  T E S T S
  [INFO] -------------------------------------------------------
  [INFO] Running com.build.VersionInfoTest
  [INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time   elapsed: 0.002 s - in com.build.VersionInfoTest
  [INFO] 
  [INFO] Results:
  [INFO] 
  [INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
  [INFO] 
  [INFO] ------------------------------------------------------------------------
  [INFO] BUILD SUCCESS
  [INFO] ------------------------------------------------------------------------
  [INFO] Total time: 1.955 s
  [INFO] Finished at: 2020-03-09T10:00:22-04:00
  [INFO] ------------------------------------------------------------------------


    <packaging>jar</packaging>
<properties>
    <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.0</junit.jupiter.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
             <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M4</version>
        </plugin>
    </plugins>
</build>

  package com.dbb.build

  import static org.junit.jupiter.api.Assertions.*;
  import org.junit.jupiter.api.Test;
  import com.dbb.build.VersionInfo;

  class VersionInfoTest {

VersionInfo versionInfo = VersionInfo.getInstance();

@Test
void getVersion() {
    String version = versionInfo.getVersion();
    System.out.println(version);
    assertNotNull(versionInfo.getVersion(), "expected a return value of"+version+"but was null");
}
  }

更新:

  [INFO] --- maven-resources-plugin:2.6:testResources (default- testResources) @ DBB-Unit-Testing ---
  [INFO] Using 'UTF-8' encoding to copy filtered resources.
  [INFO] Copying 0 resource
  [INFO] 
  [INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ DBB-Unit-Testing ---
  [INFO] Changes detected - recompiling the module!
  [INFO] Compiling 1 source file to /Unit-Testing/target/test-classes
  [INFO] -------------------------------------------------------------
  [ERROR] COMPILATION ERROR : 
  [INFO] -------------------------------------------------------------
  [ERROR] /Unit-Testing/src/test/java/com/VersionInfoTest.java:[7,25]  cannot find symbol
   symbol:   class VersionInfo
   location: package com.build
  [ERROR] /Unit-Testing/src/test/java/com/build/VersionInfoTest.java: [11,9] cannot find symbol
  symbol:   class VersionInfo
  location: class com.build.TestVersionInfo
 [ERROR] /Unit-Testing/src/test/java/com/ /build/VersionInfoTest.java:  [11,35] cannot find symbol
   symbol:   variable VersionInfo
   location: class com.build.TestVersionInfo
   [INFO] 3 errors

解决方案:使用 junit-platform-console-standalone-1.5.2.jar 并通过命令行运行单元。看起来如果我们有一个非 maven 项目 junit-platform-console-standalone 似乎是一个更好的选择。

【问题讨论】:

  • 您能否添加您在帖子中提到的错误?因为我没有看到错误?
  • com.build.VersionInfoTest 已用时间:0.002 s java.lang.NoClassDefFoundError: Lcom/build/VersionInfo;引起:java.lang.ClassNotFoundException:com.build.VersionInfo
  • 那么你的构建被破坏了..请完整的错误消息不仅仅是摘录......

标签: maven junit5


【解决方案1】:

这是我在 Maven 3.x 中使用的一些 pom 的示例,以及在 Eclipse 中使用 JUnit 5 按预期执行的测试,也可以从命令行执行:

不要添加太多 Juniper 工件,如果存在一些会产生一些副作用。

还要注意更新版本的surefire插件在过去与JUnit5有一些问题

    <properties>
        <!-- ensure proper encoding of source and resource files in the project -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <junit-5.version>5.6.0</junit-5.version>
    </properties>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M4</version>
            </plugin>
        </plugins>
    </build>


更新:

你可以在这里找到一个小例子:https://gist.github.com/asa-git/8e34bbc51b5fcb09b7fab3efdaaa73c9

请注意,我使用的是 maven 版本 3.6.3 和 JDK 8。

此外,在 Windows 上从命令行运行时(但在其他系统上也是如此),您需要确保您的 JDK 在您的系统上安装任何其他 JSE 之前位于您的路径上。

【讨论】:

  • 谢谢。我在上面更新了我的 cmets。现在没有发现任何测试?
  • 你能分享一个你的程序的简单版本吗?
  • asa,我添加了我尝试使用 junit5 和 maven 运行的测试。我导入了 java 类所在的项目(import com.dbb.build.VersionInfo)并获得了我正在为其编写 junit 测试的方法的实例。
  • 我尝试为其编写测试的项目是否使用 javase-1.7 也很重要。我为编写 junit 测试而创建的 maven 项目正在使用 javase-1.8?
  • 我回家后创建一个小测试项目放在github上供大家下载试用。
猜你喜欢
  • 1970-01-01
  • 2019-07-17
  • 1970-01-01
  • 2013-12-04
  • 2019-03-05
  • 2014-05-22
  • 2013-02-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多