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
9 changed files
with
211 additions
and
20 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
114 changes: 114 additions & 0 deletions
114
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,114 @@ | ||
/* | ||
* 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 Ticket #1405"() { | ||
setup: | ||
A a = Stub(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: | ||
A a = GroovyStub(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: | ||
C c = Stub(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: | ||
C c = GroovyStub(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: | ||
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() | ||
} | ||
} |
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