我正在尝试从Junit 4升级到Junit 5,所以我们在测试中使用了Mockito,所以要替换Run,我们必须添加@ExtendWith(MockitoExtension::class)
,并将其添加到依赖项中
testImplementation("org.mockito:mockito-junit-jupiter:3.3.3")
我们在gradle里有这个,我们加了这个
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
exclude(group = "junit", module = "junit")
}
testImplementation("org.junit.jupiter:junit-jupiter:5.6.2")
在我们使用Mockito运行这个测试类之后,我们得到了这个错误
org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-vintage' failed to discover tests
java.lang.NoSuchMethodError: 'org.junit.platform.engine.EngineDiscoveryListener org.junit.platform.engine.EngineDiscoveryRequest.getDiscoveryListener()'
at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.resolveCompletely(EngineDiscoveryRequestResolution.java:88)
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.vintage.engine.discovery.VintageDiscoverer.discover(VintageDiscoverer.java:44)
at org.junit.vintage.engine.VintageTestEngine.discover(VintageTestEngine.java:63)
at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:168)
at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:155)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
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)
我看到了这个question (JUnit 5 and Mockito)并验证了答案,但我的回答也不起作用
更新1
这是整个build.gradle
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.1.7.RELEASE"
id("io.spring.dependency-management") version "1.0.8.RELEASE"
kotlin("jvm") version "1.3.50"
kotlin("plugin.spring") version "1.3.50"
}
group = "-"
version = System.getenv("VERSION") ?: "local"
java.sourceCompatibility = JavaVersion.VERSION_12
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
.
.
.
.
testImplementation("org.junit.jupiter:junit-jupiter:5.6.2")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
exclude(group = "junit", module = "junit")
}
testImplementation("org.mockito:mockito-junit-jupiter:3.3.3")
}
发布于 2020-04-29 11:29:47
据我所见,您的build.gradle
有几个问题:
mockito-depedency
plugins
中的显式版本,并抱怨在plugins
中使用kotlin()
。请更改为id dependencies->
在下面的build.gradle
中,我修复了这些问题:
plugins {
id 'org.springframework.boot' version '2.1.7.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.3.50'
}
group = "-"
version = System.getenv("VERSION") ?: "local"
java.sourceCompatibility = JavaVersion.VERSION_12
repositories {
mavenCentral()
}
test {
useJUnitPlatform()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
testImplementation("org.junit.jupiter:junit-jupiter:5.6.2")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude group : "org.junit.vintage", module : "junit-vintage-engine"
exclude group : "junit", module : "junit"
}
testImplementation("org.mockito:mockito-junit-jupiter")
}
https://stackoverflow.com/questions/61498720
复制相似问题