使用startsWith或like检查价差运算符的结果

时间:2018-11-07 12:05:45

标签: groovy spock

假设我有:

class Conflict extends RuntimeException {
    Conflict(String message) {
        super(message)
    }
}

class Conflicts extends RuntimeException {

    List<Conflict> conflicts = new ArrayList<>()

    Conflicts(List<Conflict> conflicts) {
        this.conflicts = conflicts
    }
}

Spock中,我通常使用spread operator检查异常消息:

def e = thrown(Conflicts)
e.conflicts*.message == ["my expected message 1", "my expected message 2"]

在Groovy中,有没有办法以类似简洁的方式检查startsWithlike和其他操作?

1 个答案:

答案 0 :(得分:1)

您可以使用every

def e = thrown(Conflicts)
e.conflicts*.message.every { it.startsWith("my expected message" }