首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数据驱动Spock测试

数据驱动Spock测试
EN

Stack Overflow用户
提问于 2020-09-18 23:55:17
回答 1查看 92关注 0票数 0

我正在做一些非常基本的Spock刷新训练,我正在尝试做一个数据驱动的测试。以下是规格说明:

代码语言:javascript
复制
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"
    }
}

每次我运行它时,我都会得到这样的错误:

Error Message

我已经阅读了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

也就是这个:

代码语言:javascript
复制
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编译器,测试运行器,但不知道还能去哪里看。再说一次,任何想法都会受到欢迎。谢谢。

EN

回答 1

Stack Overflow用户

发布于 2020-09-21 08:57:04

您希望在Spock测试中去掉JUnit @Test注释,然后它将使用或不使用feature方法参数。以下是您的规范的一个不太冗长且更"spockish“的版本:

代码语言:javascript
复制
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版本中使用它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63959033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档