forked from spockframework/spock
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Defining responses for additionalInterfaces doesn't work
The metaClass used in JavaMocks of Groovy classes did not contain the methods of additionalInterfaces, so the fix is to change the MetaClass, if we are a GroovyObject and have additionalInterfaces to the mocked MetaClass. It is not the best solution, because we now use the MetaClass of the Mock, which also contains methods and interfaces of Spock internal, but there is currently no class implementing mockType + additionalInterfaces. This would be a bigger rework. Moved the metaClass field down to BaseMockInterceptor to use it in both cases: GroovyMockInterceptor and JavaMockInterceptor The second fix is for the JavaMock of a Java class with additionalInterfaces. Previously the mockType of the Java class was used also for the methods of the additionalInterfaces, which lead to a NullPointerException. This fixes spockframework#1405
- Loading branch information
Showing
7 changed files
with
165 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
spock-specs/src/test/groovy/org/spockframework/mock/AdditionalInterfaceResponseSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package org.spockframework.mock | ||
|
||
import spock.lang.Issue | ||
import spock.lang.Specification | ||
|
||
import java.util.function.IntSupplier | ||
|
||
class AdditionalInterfaceResponseSpec extends Specification { | ||
|
||
@Issue("https://github.com/spockframework/spock/issues/1405") | ||
def "Defining responses for additionalInterfaces for Groovy class Ticket #1405"() { | ||
setup: | ||
def a = Stub(A, additionalInterfaces: [B]) { | ||
it.methodFromA() >> { "MockedA" } | ||
it.methodFromIf() >> { "Result" } | ||
} | ||
expect: | ||
a instanceof A | ||
a.methodFromA() == "MockedA" | ||
a instanceof B | ||
B ifType = a as B | ||
ifType.methodFromIf() == "Result" | ||
a.methodFromIf() == "Result" | ||
} | ||
|
||
def "Defining responses for additionalInterfaces for Groovy class with GroovyStub"() { | ||
setup: | ||
def a = GroovyStub(A, additionalInterfaces: [B]) { | ||
it.methodFromA() >> { "MockedA" } | ||
it.methodFromIf() >> { "Result" } | ||
} | ||
expect: | ||
a instanceof A | ||
a.methodFromA() == "MockedA" | ||
a instanceof B | ||
a.methodFromIf() == "Result" | ||
B ifType = (B) a | ||
ifType.methodFromIf() == "Result" | ||
} | ||
|
||
@Issue("https://github.com/spockframework/spock/issues/1405") | ||
def "Defining responses for additionalInterfaces for Groovy interface Ticket #1405"() { | ||
setup: | ||
def c = Stub(C, additionalInterfaces: [B]) { | ||
it.methodFromIfC() >> { "ResultC" } | ||
it.methodFromIf() >> { "ResultB" } | ||
} | ||
expect: | ||
c instanceof C | ||
(c as C).methodFromIfC() == "ResultC" | ||
c instanceof B | ||
B ifType = c as B | ||
ifType.methodFromIf() == "ResultB" | ||
c.methodFromIf() == "ResultB" | ||
} | ||
|
||
def "Defining responses for additionalInterfaces for Groovy interface with GroovyStub"() { | ||
setup: | ||
def c = GroovyStub(C, additionalInterfaces: [B]) { | ||
it.methodFromIfC() >> { "ResultC" } | ||
it.methodFromIf() >> { "ResultB" } | ||
} | ||
expect: | ||
c instanceof C | ||
c.methodFromIfC() == "ResultC" | ||
c instanceof B | ||
c.methodFromIf() == "ResultB" | ||
B ifType = (B) c | ||
ifType.methodFromIf() == "ResultB" | ||
} | ||
|
||
@Issue("https://github.com/spockframework/spock/issues/1405") | ||
def "Defining responses for additionalInterfaces for Java classes Ticket #1405"() { | ||
setup: | ||
def a = Stub(ArrayList, additionalInterfaces: [IntSupplier]) { | ||
it.getAsInt() >> { 5 } | ||
} | ||
expect: | ||
a instanceof ArrayList | ||
a instanceof IntSupplier | ||
a.getAsInt() == 5 | ||
} | ||
|
||
static class A { | ||
@SuppressWarnings('GrMethodMayBeStatic') | ||
String methodFromA() { | ||
return "RealA" | ||
} | ||
} | ||
|
||
interface B { | ||
String methodFromIf() | ||
} | ||
|
||
interface C { | ||
String methodFromIfC() | ||
} | ||
} |