Skip to content

Commit

Permalink
Merging constructor mocks with safe accessibility whilst fixing use o…
Browse files Browse the repository at this point in the history
…f reflection where new deprecations are better considered.
  • Loading branch information
raphw committed Aug 14, 2020
1 parent 8bc21ed commit 5b4e612
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 26 deletions.
Expand Up @@ -64,7 +64,7 @@ public IMockitoConfiguration loadConfiguration() {
}

try {
return (IMockitoConfiguration) configClass.newInstance();
return (IMockitoConfiguration) configClass.getDeclaredConstructor().newInstance();
} catch (ClassCastException e) {
throw new MockitoConfigurationException(
"MockitoConfiguration class must implement "
Expand Down
Expand Up @@ -79,7 +79,7 @@ private <T> T create(Class<T> pluginType, String className) {
// Default implementation. Use our own ClassLoader instead of the context
// ClassLoader, as the default implementation is assumed to be part of
// Mockito and may not be available via the context ClassLoader.
return pluginType.cast(Class.forName(className).newInstance());
return pluginType.cast(Class.forName(className).getDeclaredConstructor().newInstance());
} catch (Exception e) {
throw new IllegalStateException(
"Internal problem occurred, please report it. "
Expand Down
Expand Up @@ -47,7 +47,7 @@ public <T> T loadImpl(Class<T> service) {
classOrAlias = plugins.getDefaultPluginClass(alias);
}
Class<?> pluginClass = loader.loadClass(classOrAlias);
Object plugin = pluginClass.newInstance();
Object plugin = pluginClass.getDeclaredConstructor().newInstance();
return service.cast(plugin);
}
return null;
Expand Down
Expand Up @@ -35,6 +35,7 @@
import org.mockito.invocation.MockHandler;
import org.mockito.mock.MockCreationSettings;
import org.mockito.plugins.InlineMockMaker;
import org.mockito.plugins.MemberAccessor;

import static org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.*;
import static org.mockito.internal.util.StringUtil.*;
Expand Down Expand Up @@ -569,14 +570,11 @@ public <T> T newInstance(Class<T> cls) throws InstantiationException {
for (Class<?> type : types) {
arguments[index++] = makeStandardArgument(type);
}
MemberAccessor accessor = Plugins.getMemberAccessor();
try {
if (!Modifier.isPublic(selected.getModifiers())
|| !Modifier.isPublic(cls.getModifiers())) {
selected.setAccessible(true);
}
mockitoConstruction.set(true);
try {
return (T) selected.newInstance(arguments);
return (T) accessor.newInstance(selected, arguments);
} finally {
mockitoConstruction.set(false);
}
Expand Down
Expand Up @@ -12,7 +12,6 @@
import java.lang.ref.SoftReference;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
Expand Down
Expand Up @@ -165,7 +165,7 @@ public void instantiate_fine_when_objenesis_on_the_classpath() throws Exception
classpath_with_objenesis);

// when
mock_maker_class_loaded_fine_until.newInstance();
mock_maker_class_loaded_fine_until.getConstructor().newInstance();

// then everything went fine
}
Expand Down
Expand Up @@ -6,7 +6,6 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.*;

import java.lang.reflect.Field;
Expand Down Expand Up @@ -128,20 +127,6 @@ public void shouldCopyValuesOfInheritedFields() throws Exception {
assertEquals(((InheritMe) from).privateInherited, ((InheritMe) to).privateInherited);
}

@Test
public void shouldEnableAndThenDisableAccessibility() throws Exception {
// given
Field privateField = SomeObject.class.getDeclaredField("privateField");
assertFalse(privateField.isAccessible());

// when
tool.copyToMock(from, to);

// then
privateField = SomeObject.class.getDeclaredField("privateField");
assertFalse(privateField.isAccessible());
}

@Test
public void shouldContinueEvenIfThereAreProblemsCopyingSingleFieldValue() throws Exception {
// given
Expand Down
Expand Up @@ -106,7 +106,7 @@ public void shouldAllowSpyingOnDefaultMethod() throws Exception {
.load(iFace.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();

Object object = spy(impl.newInstance());
Object object = spy(impl.getConstructor().newInstance());

// when
Assertions.assertThat(impl.getMethod("foo").invoke(object)).isEqualTo((Object) "bar");
Expand Down

0 comments on commit 5b4e612

Please sign in to comment.