首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在eclipse中使用maven junit5框架项目测试java项目中的方法

如何在eclipse中使用maven junit5框架项目测试java项目中的方法
EN

Stack Overflow用户
提问于 2020-03-07 10:35:36
回答 1查看 86关注 0票数 0

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

代码语言:javascript
复制
-------------------------------------------------------------------------------
   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运行项目时,是否发现了测试?

代码语言:javascript
复制
  [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>

代码语言:javascript
复制
  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");
}
  }

更新:

代码语言:javascript
复制
  [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似乎是一个更好的选择。

EN

回答 1

Stack Overflow用户

发布于 2020-03-07 15:27:29

下面是我在Maven3.x和JUnit 5中使用的一些pom的示例,这些测试按照预期执行,但也可以从命令行执行:

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

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

代码语言:javascript
复制
    <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上从命令行运行时(在其他系统上也是如此),在系统上安装任何其他JSE之前,您需要确保JDK在您的path上。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60573910

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档