【发布时间】: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'
}
【问题讨论】: