问题描述
我正在做一些非常基本的Spock复习练习,并且正在尝试进行数据驱动的测试。规格如下:
package drills
import drills.StringReverse
import spock.lang.Specification
import org.junit.Test
class TestSpockReverseString extends Specification {
@Test
def "test"(String inString,String expectedString){
given:
StringReverse systemUnderTest = new StringReverse();
when:
String actualString = systemUnderTest.reverseString(inString);
then:
expectedString.equals(actualString)
where:
inString | expectedString
null | null
"" | ""
"abc" | "cba"
"aaa" | "aaa"
"abcd" | "dcba"
"asa" | "asa"
}
}
每次运行它都会收到此错误:
我已经阅读了Spock文档并在线阅读了其他示例,看来我已经正确设置了规范。我正在运行EE Java的Eclipse IDE。 2020-03版本(4.15.0)
我需要更新某些设置以使Groovy和Spock一起正常工作吗?
任何想法都会受到赞赏。
更新: 我尝试使用此处的一种规格:
https://github.com/spockframework/spock-example/blob/master/src/test/groovy/DataDrivenSpec.groovy
即这个:
def "minimum of #a and #b is #c"() {
expect:
Math.min(a,b) == c
where:
a | b || c
3 | 7 || 3
5 | 4 || 4
9 | 9 || 9
}
与上述相同。我在想我的Eclipse设置有问题。我看过groovy编译器,测试运行程序,但不知道还要看什么。再次,任何想法将不胜感激。 谢谢。
解决方法
您要在Spock测试中摆脱JUnit @Test
批注,那么无论有没有要素方法参数,它都可以使用。这是您的规范的详细程度较低且较为“麻木的”版本:
package de.scrum_master.stackoverflow.q63959033
import spock.lang.Specification
import spock.lang.Unroll
class TestSpockReverseString extends Specification {
@Unroll
def "reversing '#inString' yields '#expectedString'"() {
expect:
expectedString == new StringReverse().reverseString(inString)
where:
inString | expectedString
null | null
"" | ""
"abc" | "cba"
"aaa" | "aaa"
"abcd" | "dcba"
"asa" | "asa"
}
static class StringReverse {
String reverseString(String string) {
string?.reverse()
}
}
}
顺便说一句,@Unroll
将是Spock 2.0的默认设置,仅在1.x版本中需要。