Mocking methods of abstract classes #1707
Answered
by
AndreasTu
Krzysztof-Lempicki
asked this question in
Q&A
-
Hi, I have class:
Is this possible with spock to run test in this manner:
? I am aware that:
I want to know if what I want to achieve is possible without above solutions. |
Beta Was this translation helpful? Give feedback.
Answered by
AndreasTu
Aug 14, 2023
Replies: 1 comment
-
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()
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
AndreasTu
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You would write someting like this when you want to use a Spy:
Or you can do it with a Mock: