java - 将数据表与交互相结合

标签 java groovy spock

有没有什么方法可以使用 Spock 的数据表来验证交互?

def test(int a, String b) {

  expect:
  service.save(a)
  1 * repository.save({ 
      def test ->
        test.value == b
  })

  where:
  a  |  b
  1  |  "one"
  2  |  "two"
}

最佳答案

是的,下面的例子工作得很好:

import spock.lang.Specification

class LolSpec extends Specification {

    def 'lol'() {
        given:
        def repository = Mock(Repository)
        def service = new Service(repository: repository)

        when:
        service.save(a)

        then:
        1 * repository.save({ it ->
            it.value == b
        })

        where:
        a | b
        1 | "one"
        2 | "two"
    }

}

class Repository {
    def save(Entity e) {

    }
}

class Service {
    Repository repository

    def save(Integer value) {
        Entity e
        if (value == 1) {
            e = new Entity(value: "one")
        } else {
            e = new Entity(value: "two")
        }
        repository.save(e)
    }
}

class Entity {
    String value
}

关于java - 将数据表与交互相结合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46534561/

相关文章:

grails - 如何在单元测试下的 Grails 服务中注入(inject)字段的模拟(使用 Autowiring )?

groovy - 使用 Spock 验证非 spy 方法调用

java - 如何检查为什么作业在 Google Dataflow 上被杀死(可能 OOM)

java - Java 中多个 Web 应用程序的单点登录

Grails域类上的java接口(interface)

java - 在 Groovy 中的集合上的 .findAll 闭包内进行赋值

unit-testing - grails 单元测试 spock 问题

java - 使用 XML 进行改造 - 元素在类中没有匹配项

Java - 根据单元格内容更改行颜色

Jenkins 工作 DSL : Using parameters in groovyScript in job step