-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix defining responses for additionalInterfaces (#1730)
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. 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 #1405 Co-authored-by: Leonard Brünings <leonard.bruenings@gradle.com>
- Loading branch information
Showing
9 changed files
with
252 additions
and
17 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
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
127 changes: 127 additions & 0 deletions
127
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,127 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
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"() { | ||
given: | ||
A a = Stub(additionalInterfaces: [B]) { | ||
it.methodFromA() >> { "MockedA" } | ||
it.methodFromIf() >> { "Result" } | ||
} | ||
|
||
expect: | ||
a instanceof A | ||
a.methodFromA() == "MockedA" | ||
a instanceof B | ||
|
||
and: | ||
B ifType = a as B | ||
ifType.methodFromIf() == "Result" | ||
a.methodFromIf() == "Result" | ||
} | ||
|
||
def "Defining responses for additionalInterfaces for Groovy class with GroovyStub"() { | ||
given: | ||
A a = GroovyStub(additionalInterfaces: [B]) { | ||
it.methodFromA() >> { "MockedA" } | ||
it.methodFromIf() >> { "Result" } | ||
} | ||
|
||
expect: | ||
a instanceof A | ||
a.methodFromA() == "MockedA" | ||
a instanceof B | ||
a.methodFromIf() == "Result" | ||
|
||
and: | ||
B ifType = (B) a | ||
ifType.methodFromIf() == "Result" | ||
} | ||
|
||
@Issue("https://github.com/spockframework/spock/issues/1405") | ||
def "Defining responses for additionalInterfaces for Groovy interface"() { | ||
given: | ||
C c = Stub(additionalInterfaces: [B]) { | ||
it.methodFromIfC() >> { "ResultC" } | ||
it.methodFromIf() >> { "ResultB" } | ||
} | ||
|
||
expect: | ||
c instanceof C | ||
(c as C).methodFromIfC() == "ResultC" | ||
c instanceof B | ||
|
||
and: | ||
B ifType = c as B | ||
ifType.methodFromIf() == "ResultB" | ||
c.methodFromIf() == "ResultB" | ||
} | ||
|
||
def "Defining responses for additionalInterfaces for Groovy interface with GroovyStub"() { | ||
given: | ||
C c = GroovyStub(additionalInterfaces: [B]) { | ||
it.methodFromIfC() >> { "ResultC" } | ||
it.methodFromIf() >> { "ResultB" } | ||
} | ||
|
||
expect: | ||
c instanceof C | ||
c.methodFromIfC() == "ResultC" | ||
c instanceof B | ||
c.methodFromIf() == "ResultB" | ||
|
||
and: | ||
B ifType = (B) c | ||
ifType.methodFromIf() == "ResultB" | ||
} | ||
|
||
@Issue("https://github.com/spockframework/spock/issues/1405") | ||
def "Defining responses for additionalInterfaces for Java classes"() { | ||
given: | ||
ArrayList a = Stub(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() | ||
} | ||
} |
Oops, something went wrong.