gpt4 book ai didi

KotlinTest 的 BehaviorSpec "No runnable methods"与 SpringJUnit4ClassRunner

转载 作者:行者123 更新时间:2023-12-02 12:55:06 25 4
gpt4 key购买 nike

我写了以下测试:

@RunWith(SpringJUnit4ClassRunner::class)
class KotlinTest : BehaviorSpec() {
init {
given("a stack") {
val stack = Stack<String>()
`when`("an item is pushed") {
stack.push("kotlin")
then("the stack should not be empty") {
stack.isEmpty() shouldBe true
}
}
`when`("the stack is popped") {
stack.pop()
then("it should be empty") {
stack.isEmpty() shouldBe false
}
}
}
}
}

当我尝试运行它时,出现以下错误:
java.lang.Exception: No runnable methods

at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:128)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:138)

我知道问题在于我正在尝试使用带有 Spring 的 KotlinTest 编写测试,但我该怎么做呢?我应该使用什么运行者?

示例测试不需要spring,这只是我为了隔离问题而写的一个简单示例

最佳答案

您在构造函数中编写了测试。测试应该用 @Test 的函数编写。注解。您的代码更不等同于:

@RunWith(SpringJUnit4ClassRunner.class)
public final class JavaTest extends BehaviorSpec {

public JavaTest() {
// given stack, when item is pushed, ...
}
}

你想要的是
@RunWith(SpringJUnit4ClassRunner.class)
public final class JavaTest extends BehaviorSpec {

@Test
public void testName() {
// given stack, when item is pushed, ...
}
}

为此,您需要使用 fun定义函数的关键字。结果应该看起来不像这样:
@RunWith(SpringJUnit4ClassRunner::class)
class KotlinTest : BehaviorSpec() {

@Test fun testName() {
given stack, when item is pushed, ...
}
}

关于KotlinTest 的 BehaviorSpec "No runnable methods"与 SpringJUnit4ClassRunner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44855083/

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