Skip to content

Commit

Permalink
Merge branch 'main' into issue_2626
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/org/mockito/internal/stubbing/defaultanswers/ReturnsSmartNulls.java
  • Loading branch information
JojOatXGME committed Aug 30, 2022
2 parents ecbdd54 + 530558a commit 072d420
Show file tree
Hide file tree
Showing 35 changed files with 695 additions and 176 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Expand Up @@ -11,16 +11,16 @@ buildscript {

classpath "io.github.gradle-nexus:publish-plugin:1.1.0"
classpath 'org.shipkit:shipkit-changelog:1.2.0'
classpath 'org.shipkit:shipkit-auto-version:1.2.0'
classpath 'org.shipkit:shipkit-auto-version:1.2.1'

classpath 'com.google.googlejavaformat:google-java-format:1.15.0'
classpath 'com.android.tools.build:gradle:4.2.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10"
}
}

plugins {
id 'com.diffplug.spotless' version '6.8.0'
id 'com.diffplug.spotless' version '6.10.0'
id 'eclipse'
id 'com.github.ben-manes.versions' version '0.42.0'
id 'biz.aQute.bnd.builder' version '6.3.1'
Expand Down
10 changes: 5 additions & 5 deletions gradle/dependencies.gradle
Expand Up @@ -4,13 +4,13 @@ ext {

def versions = [:]

versions.bytebuddy = '1.12.12'
versions.junitJupiter = '5.8.2'
versions.bytebuddy = '1.12.14'
versions.junitJupiter = '5.9.0'
versions.errorprone = '2.14.0'

libraries.junit4 = 'junit:junit:4.13.2'
libraries.junitJupiterApi = "org.junit.jupiter:junit-jupiter-api:${versions.junitJupiter}"
libraries.junitPlatformLauncher = 'org.junit.platform:junit-platform-launcher:1.8.2'
libraries.junitPlatformLauncher = 'org.junit.platform:junit-platform-launcher:1.9.0'
libraries.junitJupiterEngine = "org.junit.jupiter:junit-jupiter-engine:${versions.junitJupiter}"
libraries.junitVintageEngine = "org.junit.vintage:junit-vintage-engine:${versions.junitJupiter}"
libraries.assertj = 'org.assertj:assertj-core:3.23.1'
Expand All @@ -32,9 +32,9 @@ libraries.osgi = 'org.osgi:osgi.core:8.0.0'
libraries.equinox = 'org.eclipse.platform:org.eclipse.osgi:3.18.0'
libraries.bndGradle = 'biz.aQute.bnd:biz.aQute.bnd.gradle:6.3.1'

libraries.groovy = 'org.codehaus.groovy:groovy:3.0.11'
libraries.groovy = 'org.codehaus.groovy:groovy:3.0.12'

def kotlinVersion = '1.7.0'
def kotlinVersion = '1.7.10'
libraries.kotlin = [
version: kotlinVersion,

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/mockito/Mockito.java
Expand Up @@ -1259,19 +1259,19 @@
* void receive(String item);
*
* // Java 8 - style 1
* doAnswer(AdditionalAnswers.<String,Callback>answerVoid((operand, callback) -> callback.receive("dummy"))
* doAnswer(AdditionalAnswers.<String,Callback>answerVoid((operand, callback) -> callback.receive("dummy")))
* .when(mock).execute(anyString(), any(Callback.class));
*
* // Java 8 - style 2 - assuming static import of AdditionalAnswers
* doAnswer(answerVoid((String operand, Callback callback) -> callback.receive("dummy"))
* doAnswer(answerVoid((String operand, Callback callback) -> callback.receive("dummy")))
* .when(mock).execute(anyString(), any(Callback.class));
*
* // Java 8 - style 3 - where mocking function to is a static member of test class
* private static void dummyCallbackImpl(String operation, Callback callback) {
* callback.receive("dummy");
* }
*
* doAnswer(answerVoid(TestClass::dummyCallbackImpl)
* doAnswer(answerVoid(TestClass::dummyCallbackImpl))
* .when(mock).execute(anyString(), any(Callback.class));
*
* // Java 7
Expand All @@ -1287,7 +1287,7 @@
*
* // this could be mocked
* // Java 8
* doAnswer(AdditionalAnswers.<Boolean,String,String>answer((input1, input2) -> input1.equals(input2))))
* doAnswer(AdditionalAnswers.<Boolean,String,String>answer((input1, input2) -> input1.equals(input2)))
* .when(mock).execute(anyString(), anyString());
*
* // Java 7
Expand Down
Expand Up @@ -25,4 +25,29 @@ public interface StackTraceCleaner {
* @return whether the element should be excluded from cleaned stack trace.
*/
boolean isIn(StackTraceElement candidate);

/**
* It's recommended to override this method in subclasses to avoid potentially costly re-boxing operations.
*/
default boolean isIn(StackFrameMetadata candidate) {
return isIn(
new StackTraceElement(
candidate.getClassName(),
candidate.getMethodName(),
candidate.getFileName(),
candidate.getLineNumber()));
}

/**
* Very similar to the StackFrame class declared on the StackWalker api.
*/
interface StackFrameMetadata {
String getClassName();

String getMethodName();

String getFileName();

int getLineNumber();
}
}
Expand Up @@ -11,7 +11,7 @@

import org.mockito.MockedConstruction;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.debugging.LocationImpl;
import org.mockito.internal.debugging.LocationFactory;
import org.mockito.invocation.Location;
import org.mockito.plugins.MockMaker;

Expand All @@ -21,7 +21,7 @@ public final class MockedConstructionImpl<T> implements MockedConstruction<T> {

private boolean closed;

private final Location location = new LocationImpl();
private final Location location = LocationFactory.create();

protected MockedConstructionImpl(MockMaker.ConstructionMockControl<T> control) {
this.control = control;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/mockito/internal/MockedStaticImpl.java
Expand Up @@ -17,7 +17,7 @@
import org.mockito.Mockito;
import org.mockito.exceptions.base.MockitoAssertionError;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.debugging.LocationImpl;
import org.mockito.internal.debugging.LocationFactory;
import org.mockito.internal.listeners.VerificationStartedNotifier;
import org.mockito.internal.progress.MockingProgress;
import org.mockito.internal.stubbing.InvocationContainerImpl;
Expand All @@ -35,7 +35,7 @@ public final class MockedStaticImpl<T> implements MockedStatic<T> {

private boolean closed;

private final Location location = new LocationImpl();
private final Location location = LocationFactory.create();

protected MockedStaticImpl(MockMaker.StaticMockControl<T> control) {
this.control = control;
Expand Down
Expand Up @@ -43,7 +43,7 @@ private IMockitoConfiguration createConfig() {
}

public static void validate() {
new GlobalConfiguration();
GlobalConfiguration unused = new GlobalConfiguration();
}

public org.mockito.plugins.AnnotationEngine tryGetPluginAnnotationEngine() {
Expand Down
Expand Up @@ -51,7 +51,7 @@
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.internal.creation.bytebuddy.inject.MockMethodDispatcher;
import org.mockito.internal.debugging.LocationImpl;
import org.mockito.internal.debugging.LocationFactory;
import org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter;
import org.mockito.internal.invocation.RealMethod;
import org.mockito.internal.invocation.SerializableMethod;
Expand Down Expand Up @@ -132,11 +132,7 @@ public Callable<?> handle(Object instance, Method origin, Object[] arguments) th
}
return new ReturnValueWrapper(
interceptor.doIntercept(
instance,
origin,
arguments,
realMethod,
new LocationImpl(new Throwable(), true)));
instance, origin, arguments, realMethod, LocationFactory.create(true)));
}

@Override
Expand All @@ -154,7 +150,7 @@ public Callable<?> handleStatic(Class<?> type, Method origin, Object[] arguments
origin,
arguments,
new StaticMethodCall(selfCallInfo, type, origin, arguments),
new LocationImpl(new Throwable(), true)));
LocationFactory.create(true)));
}

@Override
Expand Down
Expand Up @@ -22,7 +22,7 @@
import net.bytebuddy.implementation.bind.annotation.StubValue;
import net.bytebuddy.implementation.bind.annotation.SuperCall;
import net.bytebuddy.implementation.bind.annotation.This;
import org.mockito.internal.debugging.LocationImpl;
import org.mockito.internal.debugging.LocationFactory;
import org.mockito.internal.invocation.RealMethod;
import org.mockito.invocation.Location;
import org.mockito.invocation.MockHandler;
Expand Down Expand Up @@ -53,7 +53,7 @@ private void readObject(ObjectInputStream stream) throws IOException, ClassNotFo

Object doIntercept(Object mock, Method invokedMethod, Object[] arguments, RealMethod realMethod)
throws Throwable {
return doIntercept(mock, invokedMethod, arguments, realMethod, new LocationImpl());
return doIntercept(mock, invokedMethod, arguments, realMethod, LocationFactory.create());
}

Object doIntercept(
Expand Down
Expand Up @@ -5,7 +5,7 @@
package org.mockito.internal.creation.proxy;

import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.debugging.LocationImpl;
import org.mockito.internal.debugging.LocationFactory;
import org.mockito.internal.invocation.RealMethod;
import org.mockito.internal.util.Platform;
import org.mockito.invocation.MockHandler;
Expand Down Expand Up @@ -153,7 +153,12 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
return handler.get()
.handle(
createInvocation(
proxy, method, args, realMethod, settings, new LocationImpl()));
proxy,
method,
args,
realMethod,
settings,
LocationFactory.create()));
}
}

Expand Down
Expand Up @@ -9,7 +9,7 @@
import org.mockito.internal.exceptions.stacktrace.StackTraceFilter;
import org.mockito.invocation.Location;

public class LocationImpl implements Location, Serializable {
class Java8LocationImpl implements Location, Serializable {

private static final long serialVersionUID = -9054861157390980624L;
// Limit the amount of objects being created, as this class is heavily instantiated:
Expand All @@ -18,19 +18,11 @@ public class LocationImpl implements Location, Serializable {
private String stackTraceLine;
private String sourceFile;

public LocationImpl() {
this(new Throwable(), false);
}

public LocationImpl(Throwable stackTraceHolder, boolean isInline) {
public Java8LocationImpl(Throwable stackTraceHolder, boolean isInline) {
this(stackTraceFilter, stackTraceHolder, isInline);
}

public LocationImpl(StackTraceFilter stackTraceFilter) {
this(stackTraceFilter, new Throwable(), false);
}

private LocationImpl(
private Java8LocationImpl(
StackTraceFilter stackTraceFilter, Throwable stackTraceHolder, boolean isInline) {
computeStackTraceInformation(stackTraceFilter, stackTraceHolder, isInline);
}
Expand Down

0 comments on commit 072d420

Please sign in to comment.