Kotlin Micronaut Mockk未找到答案例外

时间:2020-02-05 09:52:37

标签: kotlin rx-java2 micronaut mockk

我是Micronaut框架的新手,正在尝试测试我创建的演示示例。

我遇到错误io.mockk.MockKException: no answer found for: MathFacade(#3).computeAgain(3)

我的代码就这样工作-MathService调用MathFacade。我想测试MathService,所以我在MathFacade中嘲笑MathServiceTest,只打算测试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()
    }
}

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)
    }
}

MathFacade

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)
    }
}

任何帮助将不胜感激!

0 个答案:

没有答案