我在 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 测试用例。目录。
您认为这里可能的解决方案是什么?我尝试寻找一种方法来覆盖默认测试目录,但找不到任何方法来做到这一点。
附注正如我尝试的那样,这不重复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/