Kotlin Micronaut Mock未找到答案异常
我是Micronaut框架的新手,正在尝试测试我创建的一个演示示例 我遇到错误Kotlin Micronaut Mock未找到答案异常,kotlin,rx-java2,micronaut,mockk,Kotlin,Rx Java2,Micronaut,Mockk,我是Micronaut框架的新手,正在尝试测试我创建的一个演示示例 我遇到错误io.mockk.mockkeexception:找不到以下内容的答案:MathFacade(#3).computeAgain(3) 我的代码就是这样工作的-MathService调用MathFacade。我想测试MathService,所以我在MathServiceTest中模拟MathFacade,只想测试MathService 我的代码如下: MathServiceTest package io.micronau
io.mockk.mockkeexception:找不到以下内容的答案:MathFacade(#3).computeAgain(3)
我的代码就是这样工作的-MathService
调用MathFacade
。我想测试MathService
,所以我在MathServiceTest
中模拟MathFacade
,只想测试MathService
我的代码如下:
MathServiceTest
package io.micronaut.test.kotlintest
import io.kotlintest.specs.BehaviorSpec
import io.micronaut.test.annotation.MicronautTest
import io.micronaut.test.annotation.MockBean
import io.micronaut.test.extensions.kotlintest.MicronautKotlinTestExtension.getMock
import io.mockk.mockk
import io.mockk.every
import io.reactivex.Single
import kotlin.math.pow
import kotlin.math.roundToInt
@MicronautTest
class MathServiceTest(
private val mathService: MathService,
private val mathFacade: MathFacade
): BehaviorSpec({
given("the math service") {
`when`("the service is called with 3") {
val mock = getMock(mathFacade)
every { mock.computeAgain(any()) } answers {
Single.just(firstArg<Int>().toDouble().pow(3).roundToInt())
}
val result = mathService.compute(2)
then("the result is 9") {
mathService.compute(3).test().assertResult(27)
}
}
}
}) {
@MockBean(MathFacade::class)
fun mathFacade(): MathFacade {
return mockk()
}
}
封装io.micronaut.test.kotlintest
导入io.kotlintest.specs.BehaviorSpec
导入io.micronaut.test.annotation.MicronautTest
导入io.micronaut.test.annotation.MockBean
导入io.micronaut.test.extensions.kotlintest.MicronautKotlinTestExtension.getMock
导入io.mockk.mockk
导入io.mock.every
导入io.reactivex.Single
导入kotlin.math.pow
导入kotlin.math.roundToInt
@微型自动测试
类MathServiceTest(
私人val mathService:mathService,
私有val mathFacade:mathFacade
):行为规范({
给定(“数学服务”){
`当`(“使用3调用服务”){
val mock=getMock(mathFacade)
每个{mock.computeAgain(any())}都有答案{
Single.just(firstArg().toDouble().pow(3.roundToInt())
}
val结果=mathService.compute(2)
然后(“结果是9”){
mathService.compute(3.test().assertResult(27)
}
}
}
}) {
@MockBean(MathFacade::类)
有趣的mathFacade():mathFacade{
返回mock()
}
}
MathService
package io.micronaut.test.kotlintest
import io.reactivex.Single
import javax.inject.Singleton
@Singleton
class MathService(private val mathFacade: MathFacade) {
fun compute(num: Int): Single<Int> {
return mathFacade.computeAgain(num)
}
}
package io.micronaut.test.kotlintest
import io.reactivex.Single
import javax.inject.Singleton
@Singleton
class MathFacade() {
fun computeAgain(num: Int): Single<Int>{
return Single.just(num*4)
}
}
封装io.micronaut.test.kotlintest
导入io.reactivex.Single
导入javax.inject.Singleton
@独生子女
类MathService(私有val mathFacade:mathFacade){
趣味计算(num:Int):单个{
返回mathFacade.computeAgain(num)
}
}
MathFacade
package io.micronaut.test.kotlintest
import io.reactivex.Single
import javax.inject.Singleton
@Singleton
class MathService(private val mathFacade: MathFacade) {
fun compute(num: Int): Single<Int> {
return mathFacade.computeAgain(num)
}
}
package io.micronaut.test.kotlintest
import io.reactivex.Single
import javax.inject.Singleton
@Singleton
class MathFacade() {
fun computeAgain(num: Int): Single<Int>{
return Single.just(num*4)
}
}
封装io.micronaut.test.kotlintest
导入io.reactivex.Single
导入javax.inject.Singleton
@独生子女
类MathFacade(){
乐趣计算增益(num:Int):单个{
返回单个。仅(num*4)
}
}
任何帮助都将不胜感激 我不知道Micronaut,但通常在模拟框架错误时,您可能没有模拟正确的对象。如果您在
mathService.compute(2)
上放置断点,则外观是否指向模拟?根据您所显示的,它看起来应该可以工作。尝试将您的代码与micronaut测试中的测试进行比较