Skip to content

The main goal of this project is to show different approached to stubbing represented by spy and mock in Spock.

Notifications You must be signed in to change notification settings

mtumilowicz/spock-spy-vs-mock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

spock-spy-vs-mock

The main goal of this project is to show different approaches to stubbing in Spock (spy vs mock).

preface

In general, difference between spy and mock is that the spy is always based on a real object, and if we call a method that was not stubbed on spy it executes exactly like on a real object.

project description

The difference between spy and mock is clearly visible when it comes to testing ArrayList:

  • mock

    def "mock"() {
        given:
        def list = Mock(ArrayList) // compile error if Mock(new ArrayList())
    
        when:
        list.add(1)
    
        then:
        list.size() == 0
    }
    
  • spy

    def "spy"() {
        given:
        def list = Spy(ArrayList) // or Spy(new ArrayList())
        
        when:
        list.add(1)
        
        then:
        list.size() == 1
    }    
    

About

The main goal of this project is to show different approached to stubbing represented by spy and mock in Spock.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages