在我的Gradle项目中,我有一个非常简单的JUnit测试:
import org.junit.jupiter.api.Test
import static org.junit.jupiter.api.Assertions.fail
class ApiCallerTest {
@Test
void testSetApiKey() {
fail();
}
}
当我在IntelliJ中运行测试时,我会得到一个任务列表,其中包含构建成功的消息。例如:
Testing started at 19:52 ...
> Task :cleanTest UP-TO-DATE
> Task :compileJava UP-TO-DATE
> Task :compileGroovy NO-SOURCE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :compileTestJava NO-SOURCE
> Task :compileTestGroovy NO-SOURCE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test NO-SOURCE
BUILD SUCCESSFUL in 0s
这不应该发生,因为我期待测试失败。我还得到一条消息“测试事件没有收到”。
plugins {
id 'groovy'
id 'java'
}
group 'com.ipdive'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.11'
testImplementation('org.junit.jupiter:junit-jupiter:5.6.0')
}
test {
useJUnitPlatform()
}
Internal Error occurred.
org.junit.platform.commons.JUnitException: TestEngine with ID 'junit-jupiter' failed to discover tests
at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:189)
at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:168)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:132)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: org.junit.platform.commons.JUnitException: MethodSelector [className = 'ApiCallerTest', methodName = 'testSetApiKey', methodParameterTypes = ''] resolution failed
at org.junit.platform.launcher.listeners.discovery.AbortOnFailureLauncherDiscoveryListener.selectorProcessed(AbortOnFailureLauncherDiscoveryListener.java:39)
at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.resolveCompletely(EngineDiscoveryRequestResolution.java:102)
at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.run(EngineDiscoveryRequestResolution.java:82)
at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolver.resolve(EngineDiscoveryRequestResolver.java:113)
at org.junit.jupiter.engine.discovery.DiscoverySelectorResolver.resolveSelectors(DiscoverySelectorResolver.java:45)
at org.junit.jupiter.engine.JupiterTestEngine.discover(JupiterTestEngine.java:69)
at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:181)
... 6 more
Caused by: org.junit.platform.commons.PreconditionViolationException: Could not load class with name: ApiCallerTest
...
...
编辑:添加了项目结构的截图。
您可以使用官方的JUnit5-Samples中的配置。在这里可以找到junit5-jupiter-starter-gradle-groovy
项目,该项目使用以下配置:
plugins {
id 'groovy'
id 'eclipse' // optional (to generate Eclipse project files)
id 'idea' // optional (to generate IntelliJ IDEA project files)
}
repositories {
mavenCentral()
}
dependencies {
implementation(localGroovy())
testImplementation('org.junit.jupiter:junit-jupiter:5.6.0')
}
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}
它可以与Gradle6.0.1、Java8和11一起工作。
为了运行测试,您可以使用以下命令:gradle test
或在IDE中配置,如图所示:
Testing started at 17:34 ...
> Task :cleanTest
> Task :compileJava NO-SOURCE
> Task :compileGroovy UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :compileTestJava UP-TO-DATE
> Task :compileTestGroovy UP-TO-DATE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedClass$3$1 (file:/C:/Users/jmoreno/.gradle/caches/modules-2/files-2.1/org.codehaus.groovy/groovy-all/2.3.11/f6b34997d04c1538ce451d3955298f46fdb4dbd4/groovy-all-2.3.11.jar) to method java.lang.Object.finalize()
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.reflection.CachedClass$3$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
com.example.project.CalculatorTestsJava > addsTwoNumbers() PASSED
com.example.project.CalculatorTestsJava > add(int, int, int)[1] PASSED
com.example.project.CalculatorTestsJava > add(int, int, int)[2] PASSED
com.example.project.CalculatorTestsJava > add(int, int, int)[3] PASSED
com.example.project.CalculatorTestsJava > add(int, int, int)[4] PASSED
com.example.project.CalculatorTests > add(int, int, int)[1] PASSED
com.example.project.CalculatorTests > add(int, int, int)[2] PASSED
com.example.project.CalculatorTests > add(int, int, int)[3] PASSED
com.example.project.CalculatorTests > add(int, int, int)[4] PASSED
com.example.project.CalculatorTests > 1 + 1 = 2() PASSED
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.0.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 3s
5 actionable tasks: 2 executed, 3 up-to-date
17:34:08: Tasks execution finished ':cleanTest :test --tests *'.
问题内容: 我们刚刚开始在我们的项目上使用Gradle和TestNG,所以我正在检查是否有任何测试失败实际上会使构建失败。我很惊讶地发现事实并非如此。正确地拾取并编译了测试,因此我看到了类文件。我也得到了运行的报告,但是显示0个测试(预期2个)。跑步给我以下内容: SuperSimpleTest.java: build.gradle包含: 我已经看过有关此主题的其他问题,并且找到了用作解决方法的提
我们刚刚开始在我们的项目中使用Gradle和TestNG,所以我正在检查失败的任何测试是否真的失败了构建。我很惊讶地看到它没有。测试被正确地拾取和编译,所以我看到了类文件。我也收到了运行报告,但它说0个测试(预计为2个)。运行给我以下信息: SuperSimpleTest.java: 生成.gradle 包含: 我已经看了一下有关此主题的其他问题,在那里我发现了使用作为解决方法的提示(请参阅 ht
我正在使用gradle来构建和测试我的应用程序。我使用命令 但是当从eclipse中运行时,测试工作完美,当使用gradle运行时失败。我的测试定义如下,它找不到的属性文件在这两个文件夹中: /home/user/Development/git/myproject/src/main/java/com/mycompany/config/home/user/Development/git/myproj
我有一个带有gradle Build Jhipster4.6.2应用程序。从IDE运行测试存在问题。测试通常从命令行执行,但通过单击fails for application context Failure(应用程序上下文失败),从STS运行测试失败: 我遵循了https://jhipster.github.io/configuring-ide-eclipse-gradle/的说明,并且可以从ST
是否可以在gradle.properties文件中定义test.maxParallelForks=runtime.runtime.availableProcessors(),而不是在test任务下的每个build.gradle文件中定义它?
问题内容: 目前,我有以下 build.gradle 文件: 这 的build.gradle 文件是我的仓库 在这里 。我所有的主文件都在 src / model /中 ,它们各自的测试在 test / model中 。 如何正确添加JUnit 4 依赖项 ,然后在 测试/模型 文件夹中运行那些测试? 问题答案: 如何正确添加junit 4依赖关系? 假设您要针对标准Maven(或等效版本)存储库