Spock Test, only check if method is called and do not execute it

前端 未结 1 1743
忘掉有多难
忘掉有多难 2021-02-04 17:00

In our Spock tests we want to check if the correct path in our software is selected. But we do not want to test the function of the methods which are called (this is done in sep

1条回答
  •  梦如初夏
    2021-02-04 17:31

    If you are not mocking the service itself, you would need to do something like this (be aware of passing the proper parameters when using metaClass:

    def "Test"() {
        setup:
            def calls = 0
            service.metaClass.innerMethod = { p1 -> calls++ }
        when:
            service.doSomething("notexisting@test.com")
        then:
            calls==1
    }
    

    and if you are mocking the service,

    def "Test"() {  
        when:
            service.doSomething("notexisting@test.com")
        then:
            1 * service.innerMethod(_)
    }
    

    0 讨论(0)
提交回复
热议问题