【问题标题】:Eclipse creates JUnit test in the wrong directoryEclipse 在错误的目录中创建 JUnit 测试
【发布时间】:2020-04-08 12:08:24
【问题描述】:

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

但是,我遇到了两个问题:

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

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 测试用例。

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

附:这是 not 重复的,因为我尝试了 this 并且我声明了所有依赖项。以下sn-p是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'
}

【问题讨论】:

    标签: java eclipse gradle junit


    【解决方案1】:

    您可以使用这个:How to create unit tests easily in eclipse(回答最多票)

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-21
      • 1970-01-01
      • 2019-03-05
      • 1970-01-01
      • 2011-04-04
      • 1970-01-01
      相关资源
      最近更新 更多