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

[ci maven-central-release] Constructor dispatch #2013

Merged
merged 2 commits into from
Aug 20, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.mockito.exceptions.misusing.MockitoConfigurationException;
import org.mockito.internal.SuppressSignatureCheck;
import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.internal.creation.instance.ConstructorInstantiator;
import org.mockito.internal.util.Platform;
import org.mockito.internal.util.concurrent.DetachedThreadLocal;
import org.mockito.internal.util.concurrent.WeakConcurrentMap;
Expand Down Expand Up @@ -258,7 +259,9 @@ public InlineByteBuddyMockMaker() {
ConstructionCallback onConstruction =
(type, object, arguments, parameterTypeNames) -> {
if (mockitoConstruction.get()) {
return currentSpied.get();
Object spy = currentSpied.get();
// Avoid that exceptions during spy creation cause class cast exceptions.
return type.isInstance(spy) ? spy : null;
} else if (currentConstruction.get() != type) {
return null;
}
Expand Down Expand Up @@ -321,16 +324,25 @@ private <T> T doCreateMock(

try {
T instance;
try {
// We attempt to use the "native" mock maker first that avoids Objenesis and Unsafe
instance = newInstance(type);
} catch (InstantiationException ignored) {
if (nullOnNonInlineConstruction) {
return null;
if (settings.isUsingConstructor()) {
instance =
new ConstructorInstantiator(
settings.getOuterClassInstance() != null,
settings.getConstructorArgs())
.newInstance(type);
} else {
try {
// We attempt to use the "native" mock maker first that avoids
// Objenesis and Unsafe
instance = newInstance(type);
} catch (InstantiationException ignored) {
if (nullOnNonInlineConstruction) {
return null;
}
Instantiator instantiator =
Plugins.getInstantiatorProvider().getInstantiator(settings);
instance = instantiator.newInstance(type);
}
Instantiator instantiator =
Plugins.getInstantiatorProvider().getInstantiator(settings);
instance = instantiator.newInstance(type);
}
MockMethodInterceptor mockMethodInterceptor =
new MockMethodInterceptor(handler, settings);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2020 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockitoinline;

import org.junit.Before;
import org.junit.Test;

import static junit.framework.TestCase.assertEquals;
import static org.mockito.Mockito.*;

public class SpyWithConstructorTest {

private SomethingAbstract somethingAbstract;

@Before
public void setUp() {
somethingAbstract = mock(SomethingAbstract.class, withSettings()
.useConstructor("foo")
.defaultAnswer(CALLS_REAL_METHODS));
}

@Test
public void shouldUseConstructor() {
assertEquals("foo", somethingAbstract.getValue());
}

static abstract class SomethingAbstract {

private final String value;

SomethingAbstract(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}
}