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 2972: Weakens visibility constraint #2983

Merged
merged 1 commit into from Apr 21, 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,7 +4,7 @@
*/
package org.mockito.internal.creation.bytebuddy;

import static net.bytebuddy.matcher.ElementMatchers.isAccessibleTo;
import static net.bytebuddy.matcher.ElementMatchers.isVisibleTo;
import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
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(isAccessibleTo(instrumentedType)));
.filter(isConstructor().and(isVisibleTo(instrumentedType)));
int arguments = Integer.MAX_VALUE;
boolean packagePrivate = true;
MethodDescription.InDefinedShape current = null;
Expand Down
Expand Up @@ -29,6 +29,7 @@
import org.mockito.Answers;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.creation.MockSettingsImpl;
import org.mockito.internal.creation.bytebuddy.sample.DifferentPackage;
import org.mockito.internal.creation.settings.CreationSettings;
import org.mockito.internal.handler.MockHandlerImpl;
import org.mockito.internal.stubbing.answers.Returns;
Expand Down Expand Up @@ -96,6 +97,22 @@ public void should_create_mock_from_accessible_inner_spy() throws Exception {
});
}

@Test
public void should_create_mock_from_visible_inner_spy() throws Exception {
MockCreationSettings<DifferentPackage> settings = settingsFor(DifferentPackage.class);
Optional<DifferentPackage> proxy =
mockMaker.createSpy(
settings,
new MockHandlerImpl<>(settings),
new DifferentPackage(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 @@ -667,18 +684,27 @@ static class Outer {

final Object p1;

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

private static class Inner extends Outer {

final Object p2;

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

public static class SamePackage {

public final Object p1;

protected SamePackage(Object p1) {
this.p1 = p1;
}
}
}
@@ -0,0 +1,17 @@
/*
* Copyright (c) 2017 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.creation.bytebuddy.sample;

import org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMakerTest;

public class DifferentPackage extends InlineDelegateByteBuddyMockMakerTest.SamePackage {

public final Object p2;

public DifferentPackage(Object p1, Object p2) {
super(p1);
this.p2 = p2;
}
}