Skip to content

Commit

Permalink
[ci maven-central-release] Merge pull request #2013 from mockito/cons…
Browse files Browse the repository at this point in the history
…tructor-dispatch

Constructor dispatch
  • Loading branch information
raphw committed Aug 20, 2020
2 parents 31bdc24 + 7e942c4 commit 7d47fad
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
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;
}
}
}

0 comments on commit 7d47fad

Please sign in to comment.