Difference between Mock / Stub / Spy in Spock test framework

前端 未结 4 747
死守一世寂寞
死守一世寂寞 2020-12-04 06:31

I don\'t understand the difference between Mock, Stub, and Spy in Spock testing and the tutorials I have been looking at online don\'t explain them in detail.

4条回答
  •  醉梦人生
    2020-12-04 06:48

    In simple terms:

    Mock: You mock a type and on the fly you get an object created. Methods in this mock object returns the default values of return type.

    Stub: You create a stub class where methods are redefined with definition as per your requirement. Ex: In real object method you call and external api and return the username against and id. In stubbed object method you return some dummy name.

    Spy: You create one real object and then you spy it. Now you can mock some methods and chose not to do so for some.

    One usage difference is you can not mock method level objects. whereas you can create a default object in method and then spy on it to get the desired behavior of methods in spied object.

提交回复
热议问题