java - Eclipse 在错误的目录中创建 JUnit 测试

标签 java eclipse gradle junit

我在 Eclipse 中有一个基于 Gradle 的项目,我想使用 JUnit 5 进行测试。它现在没有任何测试,所以我想添加一个虚拟项目(例如 assertEquals(true, true) )以确保 Gradle可以运行测试。

但是,我遇到两个问题:

1) 当我想将 JUnit 测试用例创建为“项目右键 --> 新建 --> JUnit 测试用例”时,它会在 /src 内创建一个测试用例。目录。见下图:

enter image description here

2) 当我尝试构建 Gradle 配置时,我得到以下输出:

> Task :compileJava FAILED
/home/.../src/main/java/TestFoo.java:1: error: package org.junit does not exist
import static org.junit.Assert.*;
                       ^

据我了解,测试用例无法解析包 org.junit 。同时,Eclipse 不允许我在适当的 /test 中创建适当的 JUnit 测试用例。目录。

您认为这里可能的解决方案是什么?我尝试寻找一种方法来覆盖默认测试目录,但找不到任何方法来做到这一点。

附注正如我尝试的那样,这重复this我已经声明了所有依赖项。以下代码片段是build.gradle内容(很抱歉,由于保密原因我无法显示其所有内容):


...

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {

    ...

    testCompile group: 'junit', name: 'junit', version: '4.12'
    // Reference: https://www.baeldung.com/junit-5-gradle
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
}

run {
    ...
}

test {
    useJUnitPlatform {
        includeTags 'fast'
        excludeTags 'slow'
    }
}

shadowJar {
    mergeServiceFiles()
    manifest {
        attributes 'Implementation-Title': rootProject.name
        attributes 'Implementation-Version': rootProject.version
        attributes 'Implementation-Vendor-Id': rootProject.group
        attributes 'Created-By': 'Gradle ' + gradle.gradleVersion
        attributes 'Main-Class': mainClassName
    }
    archiveName rootProject.name + '-' + rootProject.version + '.jar'
}

最佳答案

您可以使用:How to create unit tests easily in eclipse (获得最多赞成票的答案)

或者安装这个插件:https://moreunit.github.io/MoreUnit-Eclipse/ (并使用 Ctrl + J 创建测试)

关于java - Eclipse 在错误的目录中创建 JUnit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59349666/

相关文章:

eclipse - 如何修复 Buildship 中的 "Missing Gradle project configuration file"问题?

java - 断项链问题 USACO 中的错误答案

java - gradle 子项目依赖版本在根项目中被覆盖

Java,写自己的字符串拆分方法

Android Studio 找不到任何与 come.google.maps 匹配的版本

android - 当 Google Play 服务添加为依赖项时无法使用 gradle 构建

Java:在子类下使用带有枚举的switch语句

Eclipse m2e-wtp插件不断覆盖application.xml

android - Android中调试native代码的工具有哪些

Eclipse 中的 Android : Copy/Paste From LogCat (only "Text")