gpt4 book ai didi

junit - NotAMockException/如何在 Kotlin 的参数化测试中 stub 值?

转载 作者:IT老高 更新时间:2023-10-28 13:40:22 29 4
gpt4 key购买 nike

对于以下 Kotlin 类:

class ProductLogic(

private val product: Product?

) {

fun shouldShow(): Boolean {
if (product == null) {
return false
}
val version = product.version!!
if (!Utils.isAtLeastVersionX(version.major, version.minor)) {
return false
}
return true
}

}

我正在尝试在 Kotlin 中编写参数化测试:

@RunWith(ParameterizedRobolectricTestRunner::class)
@Config(constants = BuildConfig::class, sdk = [19], packageName = "com.example")
class ProductLogicTest(

private val product: Product?,
private val shouldShow: Boolean

) {

@Before
fun setUp() {
// doReturn(VERSION).`when`(product).version // (2) Raises a NotAMockException
}

@Test
fun shouldShow() {
assertThat(ProductLogic(product).shouldShow(), `is`(shouldShow))
}

companion object {
@JvmStatic
@Parameters(name = "{index}: {0} => {1}")
fun data(): Collection<Array<Any?>> {
val productMock = mock<Product>(Product::class.java)
doReturn(VERSION).`when`(productMock).version // (1) Is not applied
return asList(
arrayOf(productMock, false),
// ...
)
}

}

我想参数化 Product#version 属性的值。当我 (1) 在 data() 函数中修改它的值时,它不会在运行测试时应用。当我 (2) 尝试在 @Before 中修改其值时,会引发 NotAMockException:

org.mockito.exceptions.misusing.NotAMockException: 
Argument passed to when() is not a mock!
Example of correct stubbing:
doThrow(new RuntimeException()).when(mock).someMethod();

请注意,示例已简化 - 真正的 ProductLogic 类包含更多参数,这些参数可以使用参数化测试。

最佳答案

Robolectric 和 Mockito 版本:

testImplementation 'org.robolectric:robolectric:4.1'
testImplementation 'org.mockito:mockito-core:2.23.4'

另外,为了模拟最终类,我创建了文件 src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker 内容:

mock-maker-inline

要测试的类:

class ProductLogic(private val product: Product?) {
fun shouldShow(): Boolean {
if (product == null) {
return false
}
val version = product.version
return !isAtLeastVersionX(version.minor, version.major)
}

private fun isAtLeastVersionX(minor: Int, major: Int): Boolean {
val v = 5
return v in minor..major
}
}

class Product(val version: Version)

class Version(val minor: Int, val major: Int)

下一个测试代码适用于我并且测试通过了:

import org.hamcrest.CoreMatchers.`is`
import org.junit.Assert.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.doReturn
import org.mockito.Mockito.mock
import org.robolectric.ParameterizedRobolectricTestRunner
import org.robolectric.annotation.Config
import java.util.Arrays.asList

@RunWith(ParameterizedRobolectricTestRunner::class)
@Config(sdk = [19], packageName = "com.example")
class ProductLogicTest(private val product: Product,
private val shouldShow: Boolean) {

@Before
fun setUp() {
//doReturn(VERSION).`when`(product).version // if uncomment works fine
}

@Test
fun shouldShow() {
assertThat(ProductLogic(product).shouldShow(), `is`(shouldShow))
}


companion object {
private val VERSION = Version(1, 5)

@JvmStatic
@ParameterizedRobolectricTestRunner.Parameters(name = "{index}: {0} => {1}")
fun data(): Collection<Array<Any?>> {
val productMock = mock(Product::class.java)
doReturn(VERSION).`when`(productMock).version // Works fine
return asList(
arrayOf(productMock, false)
)
}
}
}

关于junit - NotAMockException/如何在 Kotlin 的参数化测试中 stub 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53617622/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com