当前位置: 首页 > 知识库问答 >
问题:

如何返回列表为mock in groovy spock test

锺离明煦
2023-03-14

我在从Spock Groovy mocked interface返回所需对象列表时遇到问题:

public interface SomeRepository {
    List<SomeObject> getAll();
}

所以我想在课堂上嘲弄一下:

@CompileStatic
class SomeProcessor {
    private final SomeRepository repository

    SomeProcessor(SomeRepository repository) {
        this.repository = repository
    }

    List<SomeObject> getAll() {
        return repository.all
    }
}

我有一个测试:

class SomeProcessorSpec extends Specification {
    private final SomeRepository repository = Mock(SomeRepository)

    @Subject private final SomeProcessor processor = new SomeProcessor(repository)

    def 'should collect items from repository'() {
        given:
            List<SomeObject> expected = [new SomeObject(), new SomeObject()]
            repository.all >> expected

        when:
            List<SomeObject> actual = processor.all

        then:
            assertEquals(expected, actual)
    }
}

当我尝试运行该测试时,我得到一个断言错误:

朱尼特。框架断言失败错误:应为:[com.example]。SomeObject@1fa268de,com。实例SomeOjbect@4f6ee6e4]实际值:空

所以这意味着从repository.all方法返回了null,而不是我期望的列表,这让我很困惑。问题是:使用spock和groovy测试时,如何实际返回模拟实例的列表?

共有2个答案

燕朝明
2023-03-14

根据白盒测试更好地测试完全相同的实现<代码>处理器。all返回存储库的结果。所有保持原样。所以,最好测试一下这个事实。

根据Szymon Stepniak提供的正确代码,测试可简化为:

def 'should collect items from repository'() {
    given:
        def expected = []

    when:
        def actual = processor.all

    then:
        1 * repository.all >> expected

    and: 'make sure we got the same expected list instance'
        actual.is(expected)
}

其中通过. is()验证相同的引用。

因此,不管列表中有什么,它都可以是空的。

纪实
2023-03-14

您可以尝试将存根部分移动到交互检查阶段,例如。

def 'should collect items from repository'() {
    given:
        List<SomeObject> expected = [new SomeObject(), new SomeObject()]

    when:
        List<SomeObject> actual = processor.all

    then:
        1 * repository.all >> expected

    and:
        expected == actual
}

另外,您不必使用JUnit的assertEquals——Groovy允许您使用=操作符比较这两个对象。

我已经在简单的基于Spock的应用程序中检查了您的示例,它运行良好。我用Spock0.7-groovy-2.01.0-groovy-2.41.2-groovy-2.4-SNAPSHOT对它进行了测试,并使用了所有Spock版本。无论如何,我在过去也遇到过一些类似的问题,在这些情况下,交互检查起到了关键作用。希望能有帮助。

 类似资料:
  • 问题内容: 在执行简单程序时,我注意到了这个问题。 编译错误返回为。但是在Java中是不允许的,为什么会出现这种编译错误? 我没有在这里问关于自动装箱的问题,我只是想知道如何返回。 asList的实现是 因此,将int []视为T才是这种情况的原因。 问题答案: 中的基础int没有自动自动装箱。 实际上是一个对象,而不是原始对象。 在这里返回。确实是无效的语法。 您可以使用: List examp

  • 问题内容: 我正在使用ServiceStack的Redis客户端。我有一个Lua脚本,该脚本用多个Redis调用的结果填充Lua表。我想以某种方式返回此表。我的想法是使用客户端库中的ExecLuaShaAsList方法,并在lua脚本中执行“ return myTable”。它不起作用,我总是返回一个空列表。 如何将lua表返回给Redis客户端? 这是我与Redis客户端一起使用的C#脚本: 提

  • 但是如果serviceworkList列表为空,则变量“validate”为false。我知道allMatch的规范,如果list为空,则返回为true。 有什么建议,我可以如何重建流,如果列表列表为空,我会得到假?

  • 问题内容: 我想做这样的事情: 不幸的是,列表追加未返回修改后的列表。 那么,我如何允许返回新列表? 问题答案: 不要使用追加而是串联: 这将返回一个 新 列表。不会受到影响。如果你需要有受影响的 还有 既可以使用,无论如何,然后分配分开(复印件)。

  • 问题内容: 我需要对列表进行排序,然后返回带有列表中已排序项目索引的列表。例如,如果我要排序的列表是,则需要返回。 这个问题以字节为单位发布,但我认为我会在这里重新发布。 http://bytes.com/topic/python/answers/44513-sorting-list-then-return-index- sorted-item 我的具体需求是根据对象的属性对对象列表进行排序。然后