'Mocking a Superclass in Spock

How can you unit test a class that has a superclass in Spock that invokes method calls form its superclass? Or how do you mock a superclass in Spock?

Ex:

class Bar {

  def method1(parm1){
      //Method actions
  }
}


class Foo extends Bar {

   def method2(param1, param2) {          
       //Method actions
       super.method1(param1)
   }
}

How can I mock behavior of class Bar?



Solution 1:[1]

The following code will help you to specify what methods are called actually.

setup:
def fooInstance = Spy(Foo)

when: "this try-catch block is debug code"
try {
  // do something with Foo or Bar
}
catch (Exception e) {
}

then:
0 * fooInstance._

This site may be useful. -> Spock Mock Cheatsheet

Solution 2:[2]

If you are using Spock to test Kotlin classes, you need to make the superclass methods open so that Spock can stub them.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Mike Wilklow