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

Check package-privacy of method params #2318

Merged
merged 2 commits into from Jun 9, 2021
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 @@ -25,6 +25,7 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.ArrayList;
Expand Down Expand Up @@ -97,10 +98,31 @@ private static boolean needsSamePackageClassLoader(MockFeatures<?> features) {
// package private methods.
return true;
}
if (hasNonPublicTypeReference(features.mockedType)) {
return true;
}

for (Class<?> iface : features.interfaces) {
if (!Modifier.isPublic(iface.getModifiers())) {
return true;
}
if (hasNonPublicTypeReference(iface)) {
return true;
}
}
return false;
}

private static boolean hasNonPublicTypeReference(Class<?> iface) {
for (Method method : iface.getMethods()) {
if (!Modifier.isPublic(method.getReturnType().getModifiers())) {
return true;
}
for (Class<?> param : method.getParameterTypes()) {
if (!Modifier.isPublic(param.getModifiers())) {
return true;
}
}
}
return false;
}
Expand Down
Expand Up @@ -31,10 +31,24 @@ int packagePrivateMethod() {
abstract void packagePrivateAbstractMethod();
}

public interface PublicInterfaceWithPackagePrivateMethodParam {
void doSomething(PackagePrivateInterface i);
}

public interface PublicInterfaceWithPackagePrivateMethodReturn {
PackagePrivateInterface doSomething();
}

public interface PublicInterfaceOverridesPackagePrivateMethodReturn {
PublicChildOfPackagePrivate doSomething();
}

public interface PublicInterface {}

interface PackagePrivateInterface {}

public interface PublicChildOfPackagePrivate extends PackagePrivateInterface {}

static class PackagePrivateClass {}

@Before
Expand All @@ -55,6 +69,28 @@ public void should_be_able_to_mock_package_private_method() throws Exception {
assertThat(publicClass.packagePrivateMethod()).isEqualTo(3);
}

@Test
charlesmunger marked this conversation as resolved.
Show resolved Hide resolved
public void should_be_able_to_mock_interface_method_package_private_param() throws Exception {
PublicInterfaceWithPackagePrivateMethodParam publicClass =
mock(PublicInterfaceWithPackagePrivateMethodParam.class);
publicClass.doSomething(null);
}

@Test
public void should_be_able_to_mock_interface_method_package_private_return() throws Exception {
PublicInterfaceWithPackagePrivateMethodReturn publicClass =
mock(PublicInterfaceWithPackagePrivateMethodReturn.class);
PackagePrivateInterface packagePrivateInterface = publicClass.doSomething();
}

@Test
public void should_be_able_to_mock_interface_method_package_private_return_override()
throws Exception {
PublicInterfaceOverridesPackagePrivateMethodReturn publicClass =
mock(PublicInterfaceOverridesPackagePrivateMethodReturn.class);
PackagePrivateInterface packagePrivateInterface = publicClass.doSomething();
}

@Test
public void should_be_able_to_mock_package_private_class() throws Exception {
PackagePrivateClass mock = mock(PackagePrivateClass.class);
Expand Down