Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defining responses for additionalInterfaces doesn't work #1405

Closed
boris-petrov opened this issue Dec 23, 2021 · 1 comment · Fixed by #1730
Closed

Defining responses for additionalInterfaces doesn't work #1405

boris-petrov opened this issue Dec 23, 2021 · 1 comment · Fixed by #1730
Assignees
Labels
Milestone

Comments

@boris-petrov
Copy link

Describe the bug

There is some additional context here. Check the reproduction for explanation what the bug is.

cc @leonard84

To Reproduce

class A {}
class B {}
interface C { B foo() }
...
def a = Stub(A, additionalInterfaces: [C]) {
    foo() >> { println 2; new B() }
}
println(a instanceof C)
def b = a.foo()
println b.getClass()

Expected behavior

To print true, 2 and class B.

Actual behavior

Prints true and class java.lang.Object.

Java version

openjdk version "17.0.1" 2021-10-19
OpenJDK Runtime Environment (build 17.0.1+12)
OpenJDK 64-Bit Server VM (build 17.0.1+12, mixed mode)

Buildtool version


Gradle 7.3.3

Build time: 2021-12-22 22:43:07 UTC
Revision:

Kotlin: 1.5.31
Groovy: 3.0.9
Ant: Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM: 17.0.1 (Oracle Corporation 17.0.1+12)
OS: Linux 5.15.10-arch1-1 amd64

What operating system are you using

Linux

Dependencies

Spock is the only dependency. Any version (including 2.1-M2).

Additional context

No response

@leonard84 leonard84 self-assigned this Dec 23, 2021
AndreasTu added a commit to AndreasTu/spock that referenced this issue Jul 31, 2023
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 spockframework#1405
@AndreasTu
Copy link
Member

The PR #1730 should resolve this issue.

AndreasTu added a commit to AndreasTu/spock that referenced this issue Aug 3, 2023
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 spockframework#1405
AndreasTu added a commit to AndreasTu/spock that referenced this issue Aug 7, 2023
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
AndreasTu added a commit to AndreasTu/spock that referenced this issue Aug 11, 2023
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
@AndreasTu AndreasTu assigned AndreasTu and unassigned leonard84 Aug 14, 2023
AndreasTu added a commit to AndreasTu/spock that referenced this issue Aug 21, 2023
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
AndreasTu added a commit to AndreasTu/spock that referenced this issue Aug 25, 2023
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
AndreasTu added a commit to AndreasTu/spock that referenced this issue Sep 15, 2023
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
AndreasTu added a commit to AndreasTu/spock that referenced this issue Oct 1, 2023
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
leonard84 added a commit that referenced this issue Oct 3, 2023
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>
@AndreasTu AndreasTu added this to the 2.4 milestone Feb 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants