Skip to content

Commit

Permalink
Weakens visibility constraint for spy constructors (#2983)
Browse files Browse the repository at this point in the history
To resolve the issue where spying on encapsulated objects where a super type in a different package declares only a protected constructor

Fixes #2972
  • Loading branch information
raphw committed Apr 21, 2023
1 parent 02b9634 commit baf59c4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
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;
}
}

0 comments on commit baf59c4

Please sign in to comment.