编辑:我在这里用 mockito-kotlin 创建了一张票
我有一个这样定义的类:
package me.jpalacios.poc
class MyClass(
private val myDependency: MyDependency = MyDependency()
) {
fun run() {
myDependency.doSomething()
}
}
package me.jpalacios.poc
class MyDependency {
fun doSomething() {
println("I did something")
}
}
package me.jpalacios.poc
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.verify
@ExtendWith(MockitoExtension::class)
class MyClassTest {
@Mock
private lateinit var myDependency: MyDependency
@InjectMocks
private lateinit var myClass: MyClass
@Test
fun `Test InjectMocks`() {
myClass.run()
verify(myDependency).doSomething()
}
}
看起来像这样定义的测试不起作用,因为没有注入模拟:
@ExtendWith(MockitoExtension::class)
class MyClassTest {
@Mock
private lateinit var dependency: MyDependency
@InjectMocks
private lateinit var underTest: MyClass
}
plugins {
kotlin("jvm") version "1.5.20"
}
group = "me.jpalacios"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib"))
testImplementation("org.junit.jupiter:junit-jupiter:5.7.2")
testImplementation("org.junit.jupiter:junit-jupiter-params:5.7.2")
testImplementation("org.assertj:assertj-core:3.20.2")
testImplementation("org.mockito.kotlin:mockito-kotlin:3.2.0")
testImplementation("org.mockito:mockito-junit-jupiter:3.11.2")
}
tasks{
jar {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
configurations["compileClasspath"].forEach { file: File ->
from(zipTree(file.absoluteFile))
}
}
compileKotlin {
kotlinOptions {
jvmTarget = "${JavaVersion.VERSION_11}"
}
}
test {
useJUnitPlatform()
}
}
关于为什么的任何想法?
输出是:
我做了什么
需要但未调用:myDependency.doSomething(); -> 在 me.jpalacios.poc.MyDependency.doSomething(MyDependency.kt:6) 实际上,与此模拟的交互为零。
需要但未调用:myDependency.doSomething(); -> 在 me.jpalacios.poc.MyDependency.doSomething(MyDependency.kt:6) 实际上,与此模拟的交互为零。
在 me.jpalacios.poc.MyDependency.doSomething(MyDependency.kt:6) 在 me.jpalacios.poc.MyClassTest.Test InjectMocks(MyClassTest.kt:22) 在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0( Native Method) 在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ...