Search code examples
javaunit-testinggroovyspock

Verify spock mock is called in specific order w/ specific params


Say I have a method I am unit testing

class MyClass {
    private Foo foo;

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

and the test

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?
    */
}

Solution

  • You should be able to do

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