-
Notifications
You must be signed in to change notification settings - Fork 469
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ByteBuddyMockFactory adds @internal annotation to Groovy MOP methods
The Groovy 3 & 4 runtime expects to have the @internal annotation on the MOP methods from GroovyObject. That AbstractCallSite.createGroovyObjectGetPropertySite() processes it as GroovyObject. So the ByteBuddyMockFactory now adds the @internal annotation to the intercepted MOP methods. After that the "Unable to access protected constant when spying instances" problems are gone, because we take the normal Groovy route. Also some strange inconsistencies for Groovy 2 <=> 3,4 are now gone, but a new inconsistency appeared Groovy 4 prefers is over get for boolean. But this is not a Spock issue, because in Groovy only code the same thing happens. See tests in JavaMocksForGroovyClasses. This fixes #1501
- Loading branch information
Showing
6 changed files
with
177 additions
and
21 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
11 changes: 11 additions & 0 deletions
11
spock-specs/src/test/groovy/org/spockframework/mock/AccessProtectedJavaBaseClass.java
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,11 @@ | ||
package org.spockframework.mock; | ||
|
||
class AccessProtectedJavaBaseClass { | ||
protected boolean accessible = true; | ||
} | ||
|
||
class AccessProtectedJavaSubClass extends AccessProtectedJavaBaseClass { | ||
boolean doesThisWork() { | ||
return accessible; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
spock-specs/src/test/groovy/org/spockframework/mock/AccessProtectedPropsSpec.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,49 @@ | ||
package org.spockframework.mock | ||
|
||
import org.spockframework.runtime.model.parallel.ExecutionMode | ||
import spock.lang.Execution | ||
import spock.lang.Specification | ||
|
||
@Execution(ExecutionMode.SAME_THREAD) | ||
class AccessProtectedPropsSpec extends Specification { | ||
|
||
/** | ||
* The problem with https://github.com/spockframework/spock/issues/1501 is that the ByteBuddyMockFactory overrides | ||
* the <code>getProperty(String)</code> method from GroovyObject without having the <code>groovy.transform.Internal</code> | ||
* annotation. Therefore <code>org.codehaus.groovy.runtime.callsite.AbstractCallSite.createGroovyObjectGetPropertySite(java.lang.Object)</code> | ||
* takes another code path. | ||
*/ | ||
def "Access protected const should be accessible in Groovy 3&4 Issue #1501"() { | ||
when: | ||
AccessProtectedSubClass mySpy = Spy(AccessProtectedSubClass) | ||
then: | ||
mySpy.doesThisWork() | ||
} | ||
|
||
/** | ||
* Strangely the test above, would not fail if this test has run before it. | ||
*/ | ||
def "Access protected const without spy"() { | ||
when: | ||
AccessProtectedSubClass mySpy = new AccessProtectedSubClass() | ||
then: | ||
mySpy.doesThisWork() | ||
} | ||
|
||
def "Access protected const should be accessible in Groovy 3&4 with Java class Issue #1501"() { | ||
when: | ||
AccessProtectedJavaSubClass mySpy = Spy(AccessProtectedJavaSubClass) | ||
then: | ||
mySpy.doesThisWork() | ||
} | ||
} | ||
|
||
class AccessProtectedBaseClass { | ||
protected boolean accessible = true | ||
} | ||
|
||
class AccessProtectedSubClass extends AccessProtectedBaseClass { | ||
boolean doesThisWork() { | ||
return accessible | ||
} | ||
} |
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