-
Notifications
You must be signed in to change notification settings - Fork 482
Fix Mock does not intercept package-private methods in OSGi correctly #2385
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
Open
AndreasTu
wants to merge
1
commit into
spockframework:master
Choose a base branch
from
AndreasTu:Issue2384
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
54 changes: 54 additions & 0 deletions
54
...rc/test/groovy/org/spockframework/smoke/mock/osgi/OsgiPackagePrivateMemberMockSpec.groovy
This file contains hidden or 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,54 @@ | ||
| /* | ||
| * Copyright 2026 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.smoke.mock.osgi | ||
|
|
||
|
|
||
| import spock.lang.Issue | ||
| import spock.lang.Specification | ||
| import spock.util.EmbeddedSpecRunner | ||
|
|
||
| class OsgiPackagePrivateMemberMockSpec extends Specification { | ||
| def runner = new EmbeddedSpecRunner(new OsgiTestClassLoader(this.class.classLoader)) | ||
|
|
||
| @SuppressWarnings('UnnecessaryQualifiedReference') | ||
| @Issue("https://github.com/spockframework/spock/issues/2384") | ||
| def "OSGi package private mock shall be mockable with ByteBuddy"() { | ||
| when: | ||
| def res = runner.runSpecBody(""" | ||
| def mock = Mock(org.spockframework.smoke.mock.osgi.testclasses.PkgPrivateMemberClass, mockMaker: spock.mock.MockMakers.byteBuddy) { | ||
| packagePrivate() >> "mocked" | ||
| } | ||
|
|
||
| def "mock is called when invoked from Java code"() { | ||
| when: | ||
| def result = new org.spockframework.smoke.mock.osgi.testclasses.InvocationFromJava(mock).invoke() | ||
|
|
||
| then: | ||
| result == "mocked" | ||
| } | ||
|
|
||
| def "mock is called when invoked directly from Groovy"() { | ||
| when: | ||
| def result = mock.packagePrivate() | ||
|
|
||
| then: | ||
| result == "mocked" | ||
| } | ||
| """) | ||
|
|
||
| then: | ||
| res.testsSucceededCount == 2 | ||
| } | ||
| } |
63 changes: 63 additions & 0 deletions
63
spock-specs/src/test/groovy/org/spockframework/smoke/mock/osgi/OsgiTestClassLoader.groovy
This file contains hidden or 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,63 @@ | ||
| /* | ||
| * Copyright 2026 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.smoke.mock.osgi | ||
|
|
||
| import groovy.transform.CompileStatic | ||
|
|
||
| import static java.util.Objects.requireNonNull | ||
|
|
||
| /** | ||
| * Fakes an OSGi like ClassLoader env, by not using the parent ClassLoader semantics. | ||
| */ | ||
| @CompileStatic | ||
| class OsgiTestClassLoader extends ClassLoader { | ||
| private final ClassLoader hostCl | ||
|
|
||
| OsgiTestClassLoader(ClassLoader hostCl) { | ||
| super(null) | ||
| this.hostCl = requireNonNull(hostCl) | ||
| } | ||
|
|
||
| @Override | ||
| protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { | ||
|
AndreasTu marked this conversation as resolved.
|
||
| if (!name.startsWith("org.spockframework.smoke.mock.osgi.testclasses")) { | ||
| return hostCl.loadClass(name) | ||
| } | ||
| super.loadClass(name, resolve) | ||
| } | ||
|
|
||
| @Override | ||
| protected Class<?> findClass(String name) throws ClassNotFoundException { | ||
| def clsFilePath = name | ||
| clsFilePath = clsFilePath.replace(".", "/") | ||
| clsFilePath += ".class" | ||
| def is = hostCl.getResourceAsStream(clsFilePath) | ||
| if (is == null) { | ||
| throw new ClassNotFoundException(name) | ||
| } | ||
| def clsData = is.getBytes() | ||
| return defineClass(name, clsData, 0, clsData.length) | ||
|
coderabbitai[bot] marked this conversation as resolved.
AndreasTu marked this conversation as resolved.
|
||
| } | ||
|
|
||
| @Override | ||
| URL getResource(String name) { | ||
| return hostCl.getResource(name) | ||
| } | ||
|
|
||
| @Override | ||
| Enumeration<URL> getResources(String name) throws IOException { | ||
| return hostCl.getResources(name) | ||
| } | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
...cs/src/test/groovy/org/spockframework/smoke/mock/osgi/testclasses/InvocationFromJava.java
This file contains hidden or 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,27 @@ | ||
| /* | ||
| * Copyright 2026 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.smoke.mock.osgi.testclasses; | ||
|
|
||
| public class InvocationFromJava { | ||
| PkgPrivateMemberClass obj; | ||
|
|
||
| public InvocationFromJava(PkgPrivateMemberClass obj) { | ||
| this.obj = obj; | ||
| } | ||
|
|
||
| public Object invoke() { | ||
| return obj.packagePrivate(); | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
...src/test/groovy/org/spockframework/smoke/mock/osgi/testclasses/PkgPrivateMemberClass.java
This file contains hidden or 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,22 @@ | ||
| /* | ||
| * Copyright 2026 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.smoke.mock.osgi.testclasses; | ||
|
|
||
| public class PkgPrivateMemberClass { | ||
|
|
||
| String packagePrivate() { | ||
| return "original"; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.