Skip to content

Mocking methods of abstract classes #1707

Discussion options

You must be logged in to vote

You would write someting like this when you want to use a Spy:

  def "Spy of abstract class"() {
    given:
    AbstractClass mc = Spy()
    when:
    mc.doSomething()

    then:
    //The >> null are here required to override the default Spy behavior, which would answer with a non existing method.
    1 * mc.method1() >> null 
    1 * mc.method2() >> null
  }

Or you can do it with a Mock:

  def "Mockof abstract class"() {
    given:
    AbstractClass mc = Mock() {
      doSomething() >> { callRealMethod() }
    }
    when:
    mc.doSomething()

    then:
    1 * mc.method1()
  }

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by AndreasTu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants