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

Fixes 2947: correct visibility check to respect nestmates #2948

Merged
merged 1 commit into from Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -4,8 +4,8 @@
*/
package org.mockito.internal.creation.bytebuddy;

import static net.bytebuddy.matcher.ElementMatchers.isAccessibleTo;
import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
import static net.bytebuddy.matcher.ElementMatchers.isPrivate;
import static net.bytebuddy.matcher.ElementMatchers.isStatic;
import static net.bytebuddy.matcher.ElementMatchers.not;

Expand Down Expand Up @@ -398,7 +398,7 @@ public MethodVisitor wrap(
.getSuperClass()
.asErasure()
.getDeclaredMethods()
.filter(isConstructor().and(not(isPrivate())));
.filter(isConstructor().and(isAccessibleTo(instrumentedType)));
int arguments = Integer.MAX_VALUE;
boolean packagePrivate = true;
MethodDescription.InDefinedShape current = null;
Expand Down
Expand Up @@ -80,6 +80,22 @@ public void should_create_mock_from_final_spy() throws Exception {
});
}

@Test
public void should_create_mock_from_accessible_inner_spy() throws Exception {
MockCreationSettings<Outer.Inner> settings = settingsFor(Outer.Inner.class);
Optional<Outer.Inner> proxy =
mockMaker.createSpy(
settings,
new MockHandlerImpl<>(settings),
new Outer.Inner(new Object(), new Object()));
assertThat(proxy)
.hasValueSatisfying(
spy -> {
assertThat(spy.p1).isNotNull();
assertThat(spy.p2).isNotNull();
});
}

@Test
public void should_create_mock_from_non_constructable_class() throws Exception {
MockCreationSettings<NonConstructableClass> settings =
Expand Down Expand Up @@ -646,4 +662,23 @@ void internalThrowException(int test) throws IOException {
}
}
}

static class Outer {

final Object p1;

private Outer(final Object p1) {
this.p1 = p1;
}

private static class Inner extends Outer {

final Object p2;

Inner(final Object p1, final Object p2) {
super(p1);
this.p2 = p2;
}
}
}
}