java 验证Spock模拟是否以特定顺序调用,并带有特定参数

pdsfdshx  于 2023-03-11  发布在  Java
关注(0)|答案(1)|浏览(104)

假设我有一个方法正在进行单元测试

class MyClass {
    private Foo foo;

    public void myMethod(String arg1, String arg2) {
        foo.bar(arg1);
        foo.bar(arg2);
    }
}

并且该测试

def "Playlist with stale channel should get updated upon refresh"() {
    when: "myMethod is called with 'a','b'"
    MyClass myClass = new MyClass();
    myClass.myMethod('a', 'b');

    then: "expect that foo('a') is called before foo('b') is called"
    /* 
        How can I do this? I only know how to check that it was called twice.
    */
   2 * foo.bar(_) >> null 

    /*
        How could I make an assertion like "foo.bar was called twice, first with 'a' as
          the argument, then again with 'b' as the argument?
    */
}
xdnvmnnf

xdnvmnnf1#

你应该能做到

then:
1 * foo.bar('a')

then:
1 * foo.bar('b')

相关问题