Skip to content

Commit

Permalink
Merge branch 'release/2.x' into release/2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
mockitoguy committed Mar 5, 2019
2 parents 1641868 + af2d33b commit 959cba4
Show file tree
Hide file tree
Showing 39 changed files with 1,182 additions and 344 deletions.
9 changes: 9 additions & 0 deletions .dependabot/config.yml
@@ -0,0 +1,9 @@
version: 1

update_configs:
- package_manager: "java:gradle"
directory: "/"
update_schedule: "daily"
# Redundant - default repository branch by default
target_branch: "release/2.x"

4 changes: 2 additions & 2 deletions build.gradle
Expand Up @@ -11,7 +11,7 @@ buildscript {
classpath 'net.ltgt.gradle:gradle-errorprone-plugin:0.6'

//Using buildscript.classpath so that we can resolve shipkit from maven local, during local testing
classpath 'org.shipkit:shipkit:2.0.28'
classpath 'org.shipkit:shipkit:2.1.6'
}
}

Expand Down Expand Up @@ -110,7 +110,7 @@ subprojects {
name = rootProject.name + '-' + project.name
}
}

afterEvaluate {
def lib = publishing.publications.javaLibrary
if(lib && !lib.artifactId.startsWith("mockito-")) {
Expand Down
48 changes: 48 additions & 0 deletions doc/release-notes/official.md
@@ -1,5 +1,53 @@
<sup><sup>*Release notes were automatically generated by [Shipkit](http://shipkit.org/)*</sup></sup>

#### 2.24.10
- 2019-03-05 - [4 commits](https://github.com/mockito/mockito/compare/v2.24.9...v2.24.10) by [Tim van der Lippe](https://github.com/TimvdLippe) - published to [![Bintray](https://img.shields.io/badge/Bintray-2.24.10-green.svg)](https://bintray.com/mockito/maven/mockito-development/2.24.10)
- Prevent NPE in findTypeFromGenericInArguments [(#1648)](https://github.com/mockito/mockito/pull/1648)

#### 2.24.9
- 2019-03-04 - [12 commits](https://github.com/mockito/mockito/compare/v2.24.7...v2.24.9) by 6 authors - published to [![Bintray](https://img.shields.io/badge/Bintray-2.24.9-green.svg)](https://bintray.com/mockito/maven/mockito-development/2.24.9)
- Commits: [Brice Dutheil](https://github.com/bric3) (5), [Tim van der Lippe](https://github.com/TimvdLippe) (3), [epeee](https://github.com/epeee) (1), [Fr Jeremy Krieg](https://github.com/kriegfrj) (1), [Paweł Pamuła](https://github.com/PawelPamula) (1), shipkit-org (1)
- [Java 9 support] ClassCastExceptions with JDK9 javac [(#357)](https://github.com/mockito/mockito/issues/357)
- [Bugfixes] RETURNS_DEEP_STUBS causes "Raw extraction not supported for : 'null'" in some cases [(#1621)](https://github.com/mockito/mockito/issues/1621)
- Update shipkit plugin (v2.1.6) [(#1647)](https://github.com/mockito/mockito/pull/1647)
- VerificationCollector to handle non-matching args and other assertions [(#1644)](https://github.com/mockito/mockito/pull/1644)
- VerificationCollector doesn't work for invocations with non-matching args [(#1642)](https://github.com/mockito/mockito/issues/1642)
- Fix returns mocks for final classes [(#1641)](https://github.com/mockito/mockito/pull/1641)
- Removes inaccessible links from javadocs in Mockito.java [(#1639)](https://github.com/mockito/mockito/pull/1639)
- Mockito.java contains inaccessible links to articles. [(#1638)](https://github.com/mockito/mockito/issues/1638)
- Handle terminal type var with bounds [(#1624)](https://github.com/mockito/mockito/pull/1624)
- Return null instead of causing a CCE [(#1612)](https://github.com/mockito/mockito/pull/1612)

#### 2.24.7
- 2019-02-28 - [2 commits](https://github.com/mockito/mockito/compare/v2.24.6...v2.24.7) by [shipkit-org](https://github.com/shipkit-org) (1), [Tim van der Lippe](https://github.com/TimvdLippe) (1) - published to [![Bintray](https://img.shields.io/badge/Bintray-2.24.7-green.svg)](https://bintray.com/mockito/maven/mockito-development/2.24.7)
- Fix handling of generics in ReturnsMocks [(#1635)](https://github.com/mockito/mockito/pull/1635)

#### 2.24.6
- 2019-02-27 - [16 commits](https://github.com/mockito/mockito/compare/v2.24.5...v2.24.6) by [Szczepan Faber](https://github.com/mockitoguy) (15), [Marcin Stachniuk](https://github.com/mstachniuk) (1) - published to [![Bintray](https://img.shields.io/badge/Bintray-2.24.6-green.svg)](https://bintray.com/mockito/maven/mockito-development/2.24.6)
- Make use of Shipkit v2.1.3 [(#1626)](https://github.com/mockito/mockito/pull/1626)
- Exposed new API - StubbingLookupListener [(#1543)](https://github.com/mockito/mockito/pull/1543)

#### 2.24.5
- 2019-02-18 - [2 commits](https://github.com/mockito/mockito/compare/v2.24.4...v2.24.5) by [Tim van der Lippe](https://github.com/TimvdLippe) - published to [![Bintray](https://img.shields.io/badge/Bintray-2.24.5-green.svg)](https://bintray.com/mockito/maven/mockito/2.24.5)
- No pull requests referenced in commit messages.

#### 2.24.4
- 2019-02-13 - [1 commit](https://github.com/mockito/mockito/compare/v2.24.3...v2.24.4) by [Alex Simkin](https://github.com/SimY4) - published to [![Bintray](https://img.shields.io/badge/Bintray-2.24.4-green.svg)](https://bintray.com/mockito/maven/mockito-development/2.24.4)
- Fixes #1618 : Fix strict stubbing profile serialization support. [(#1620)](https://github.com/mockito/mockito/pull/1620)
- Serializable flag doesn't make mock serializable [(#1618)](https://github.com/mockito/mockito/issues/1618)

#### 2.24.3
- 2019-02-12 - [2 commits](https://github.com/mockito/mockito/compare/v2.24.2...v2.24.3) by [Marcin Zajączkowski](https://github.com/szpak) (1), [Tim van der Lippe](https://github.com/TimvdLippe) (1) - published to [![Bintray](https://img.shields.io/badge/Bintray-2.24.3-green.svg)](https://bintray.com/mockito/maven/mockito-development/2.24.3)
- [Java 9 support] ClassCastExceptions with JDK9 javac [(#357)](https://github.com/mockito/mockito/issues/357)
- Return null instead of causing a CCE [(#1612)](https://github.com/mockito/mockito/pull/1612)
- Automatic dependency update with Dependabot [(#1600)](https://github.com/mockito/mockito/pull/1600)
- Fix/bug 1551 cce on smart not null answers [(#1576)](https://github.com/mockito/mockito/pull/1576)

#### 2.24.2
- 2019-02-11 - [1 commit](https://github.com/mockito/mockito/compare/v2.24.1...v2.24.2) by [Tim van der Lippe](https://github.com/TimvdLippe) - published to [![Bintray](https://img.shields.io/badge/Bintray-2.24.2-green.svg)](https://bintray.com/mockito/maven/mockito-development/2.24.2)
- Fix issue with mocking of java.util.* classes [(#1617)](https://github.com/mockito/mockito/pull/1617)
- Issue with mocking type in "java.util.*", Java 12 [(#1615)](https://github.com/mockito/mockito/issues/1615)

#### 2.24.1
- 2019-02-04 - [1 commit](https://github.com/mockito/mockito/compare/v2.24.0...v2.24.1) by [zoujinhe](https://github.com/zoujinhe) - published to [![Bintray](https://img.shields.io/badge/Bintray-2.24.1-green.svg)](https://bintray.com/mockito/maven/mockito-development/2.24.1)
- typo? ... 'thenReturn' instruction if completed -> ... 'thenReturn' instruction is completed [(#1608)](https://github.com/mockito/mockito/pull/1608)
Expand Down
4 changes: 4 additions & 0 deletions gradle/errorprone.gradle
Expand Up @@ -10,3 +10,7 @@ if (JavaVersion.current() == JavaVersion.VERSION_1_8) {
dependencies {
errorprone libraries.errorprone
}

tasks.named("compileTestJava").configure {
options.errorprone.errorproneArgs << "-Xep:MockitoCast:OFF"
}
19 changes: 19 additions & 0 deletions src/main/java/org/mockito/MockSettings.java
Expand Up @@ -9,6 +9,7 @@
import org.mockito.invocation.InvocationFactory;
import org.mockito.invocation.MockHandler;
import org.mockito.listeners.InvocationListener;
import org.mockito.listeners.StubbingLookupListener;
import org.mockito.listeners.VerificationStartedListener;
import org.mockito.mock.MockCreationSettings;
import org.mockito.mock.SerializableMode;
Expand Down Expand Up @@ -203,6 +204,24 @@ public interface MockSettings extends Serializable {
*/
MockSettings verboseLogging();

/**
* Add stubbing lookup listener to the mock object.
*
* Multiple listeners may be added and they will be notified orderly.
*
* For use cases and more info see {@link StubbingLookupListener}.
*
* Example:
* <pre class="code"><code class="java">
* List mockWithListener = mock(List.class, withSettings().stubbingLookupListeners(new YourStubbingLookupListener()));
* </code></pre>
*
* @param listeners The stubbing lookup listeners to add. May not be null.
* @return settings instance so that you can fluently specify other settings
* @since 2.24.6
*/
MockSettings stubbingLookupListeners(StubbingLookupListener... listeners);

/**
* Registers a listener for method invocations on this mock. The listener is
* notified every time a method on this mock is called.
Expand Down
22 changes: 10 additions & 12 deletions src/main/java/org/mockito/Mockito.java
Expand Up @@ -208,7 +208,7 @@
*
* //Although it is possible to verify a stubbed invocation, usually <b>it's just redundant</b>
* //If your code cares what get(0) returns, then something else breaks (often even before verify() gets executed).
* //If your code doesn't care what get(0) returns, then it should not be stubbed. Not convinced? See <a href="http://monkeyisland.pl/2008/04/26/asking-and-telling">here</a>.
* //If your code doesn't care what get(0) returns, then it should not be stubbed.
* verify(mockedList).get(0);
* </code></pre>
*
Expand Down Expand Up @@ -427,8 +427,7 @@
* Some users who did a lot of classic, expect-run-verify mocking tend to use <code>verifyNoMoreInteractions()</code> very often, even in every test method.
* <code>verifyNoMoreInteractions()</code> is not recommended to use in every test method.
* <code>verifyNoMoreInteractions()</code> is a handy assertion from the interaction testing toolkit. Use it only when it's relevant.
* Abusing it leads to <strong>overspecified</strong>, <strong>less maintainable</strong> tests. You can find further reading
* <a href="http://monkeyisland.pl/2008/07/12/should-i-worry-about-the-unexpected/">here</a>.
* Abusing it leads to <strong>overspecified</strong>, <strong>less maintainable</strong> tests.
*
* <p>
* See also {@link Mockito#never()} - it is more explicit and
Expand Down Expand Up @@ -599,8 +598,7 @@
* <b>Before the release 1.8</b>, Mockito spies were not real partial mocks.
* The reason was we thought partial mock is a code smell.
* At some point we found legitimate use cases for partial mocks
* (3rd party interfaces, interim refactoring of legacy code, the full article is
* <a href="http://monkeyisland.pl/2009/01/13/subclass-and-override-vs-partial-mocking-vs-refactoring">here</a>)
* (3rd party interfaces, interim refactoring of legacy code).
* <p>
*
* <pre class="code"><code class="java">
Expand Down Expand Up @@ -709,8 +707,7 @@
* <h3 id="16">16. <a class="meaningful_link" href="#partial_mocks" name="partial_mocks">Real partial mocks</a> (Since 1.8.0)</h3>
*
* Finally, after many internal debates & discussions on the mailing list, partial mock support was added to Mockito.
* Previously we considered partial mocks as code smells. However, we found a legitimate use case for partial mocks - more reading:
* <a href="http://monkeyisland.pl/2009/01/13/subclass-and-override-vs-partial-mocking-vs-refactoring">here</a>
* Previously we considered partial mocks as code smells. However, we found a legitimate use case for partial mocks.
* <p>
* <b>Before release 1.8</b> <code>spy()</code> was not producing real partial mocks and it was confusing for some users.
* Read more about spying: <a href="#13">here</a> or in javadoc for {@link Mockito#spy(Object)} method.
Expand Down Expand Up @@ -1695,6 +1692,7 @@ public class Mockito extends ArgumentMatchers {

/**
* Optional <code>Answer</code> to be used with {@link Mockito#mock(Class, Answer)}
*
* <p>
* {@link Answer} can be used to define the return values of unstubbed invocations.
* <p>
Expand Down Expand Up @@ -1726,8 +1724,11 @@ public class Mockito extends ArgumentMatchers {
* </code></pre>
*
* <p>
* <u>Note:</u> Stubbing partial mocks using <code>when(mock.getSomething()).thenReturn(fakeValue)</code>
* <u>Note 1:</u> Stubbing partial mocks using <code>when(mock.getSomething()).thenReturn(fakeValue)</code>
* syntax will call the real method. For partial mock it's recommended to use <code>doReturn</code> syntax.
* <p>
* <u>Note 2:</u> If the mock is serialized then deserialized, then this answer will not be able to understand
* generics metadata.
*/
public static final Answer<Object> CALLS_REAL_METHODS = Answers.CALLS_REAL_METHODS;

Expand Down Expand Up @@ -2080,7 +2081,6 @@ public static <T> T spy(Class<T> classToSpy) {
* Let's say you've stubbed <code>foo.bar()</code>.
* If your code cares what <code>foo.bar()</code> returns then something else breaks(often before even <code>verify()</code> gets executed).
* If your code doesn't care what <code>get(0)</code> returns then it should not be stubbed.
* Not convinced? See <a href="http://monkeyisland.pl/2008/04/26/asking-and-telling">here</a>.
*
* <p>
* See examples in javadoc for {@link Mockito} class
Expand Down Expand Up @@ -2112,7 +2112,6 @@ public static <T> OngoingStubbing<T> when(T methodCall) {
* Let's say you've stubbed <code>foo.bar()</code>.
* If your code cares what <code>foo.bar()</code> returns then something else breaks(often before even <code>verify()</code> gets executed).
* If your code doesn't care what <code>get(0)</code> returns then it should not be stubbed.
* Not convinced? See <a href="http://monkeyisland.pl/2008/04/26/asking-and-telling">here</a>.
*
* <p>
* See examples in javadoc for {@link Mockito} class
Expand Down Expand Up @@ -2214,8 +2213,7 @@ public static <T> void clearInvocations(T ... mocks) {
* Some users who did a lot of classic, expect-run-verify mocking tend to use <code>verifyNoMoreInteractions()</code> very often, even in every test method.
* <code>verifyNoMoreInteractions()</code> is not recommended to use in every test method.
* <code>verifyNoMoreInteractions()</code> is a handy assertion from the interaction testing toolkit. Use it only when it's relevant.
* Abusing it leads to overspecified, less maintainable tests. You can find further reading
* <a href="http://monkeyisland.pl/2008/07/12/should-i-worry-about-the-unexpected/">here</a>.
* Abusing it leads to overspecified, less maintainable tests.
* <p>
* This method will also detect unverified invocations that occurred before the test method,
* for example: in <code>setUp()</code>, <code>&#064;Before</code> method or in constructor.
Expand Down
28 changes: 15 additions & 13 deletions src/main/java/org/mockito/internal/creation/MockSettingsImpl.java
Expand Up @@ -11,6 +11,7 @@
import org.mockito.internal.util.MockCreationValidator;
import org.mockito.internal.util.MockNameImpl;
import org.mockito.listeners.InvocationListener;
import org.mockito.listeners.StubbingLookupListener;
import org.mockito.listeners.VerificationStartedListener;
import org.mockito.mock.MockCreationSettings;
import org.mockito.mock.MockName;
Expand All @@ -19,17 +20,17 @@

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static java.util.Arrays.asList;
import static org.mockito.internal.exceptions.Reporter.defaultAnswerDoesNotAcceptNullParameter;
import static org.mockito.internal.exceptions.Reporter.extraInterfacesAcceptsOnlyInterfaces;
import static org.mockito.internal.exceptions.Reporter.extraInterfacesDoesNotAcceptNullParameters;
import static org.mockito.internal.exceptions.Reporter.extraInterfacesRequiresAtLeastOneInterface;
import static org.mockito.internal.exceptions.Reporter.invocationListenersRequiresAtLeastOneListener;
import static org.mockito.internal.exceptions.Reporter.methodDoesNotAcceptParameter;
import static org.mockito.internal.exceptions.Reporter.requiresAtLeastOneListener;
import static org.mockito.internal.util.collections.Sets.newSet;

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -154,7 +155,7 @@ public Object[] getConstructorArgs() {
}
List<Object> resultArgs = new ArrayList<Object>(constructorArgs.length + 1);
resultArgs.add(outerClassInstance);
resultArgs.addAll(Arrays.asList(constructorArgs));
resultArgs.addAll(asList(constructorArgs));
return resultArgs.toArray(new Object[constructorArgs.length + 1]);
}

Expand All @@ -173,17 +174,23 @@ public MockSettings verboseLogging() {

@Override
public MockSettings invocationListeners(InvocationListener... listeners) {
if (listeners == null || listeners.length == 0) {
throw invocationListenersRequiresAtLeastOneListener();
}
addListeners(listeners, invocationListeners, "invocationListeners");
return this;
}

private static <T> void addListeners(T[] listeners, List<T> container, String method) {
@Override
public MockSettings stubbingLookupListeners(StubbingLookupListener... listeners) {
addListeners(listeners, stubbingLookupListeners, "stubbingLookupListeners");
return this;
}

static <T> void addListeners(T[] listeners, List<T> container, String method) {
if (listeners == null) {
throw methodDoesNotAcceptParameter(method, "null vararg array.");
}
if (listeners.length == 0) {
throw requiresAtLeastOneListener(method);
}
for (T listener : listeners) {
if (listener == null) {
throw methodDoesNotAcceptParameter(method, "null listeners.");
Expand All @@ -207,13 +214,8 @@ private boolean invocationListenersContainsType(Class<?> clazz) {
return false;
}

@Override
public List<InvocationListener> getInvocationListeners() {
return this.invocationListeners;
}

public boolean hasInvocationListeners() {
return !invocationListeners.isEmpty();
return !getInvocationListeners().isEmpty();
}

@Override
Expand Down
Expand Up @@ -46,13 +46,11 @@ 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,
new LocationImpl());
}

Object doIntercept(Object mock,
Expand Down Expand Up @@ -108,11 +106,11 @@ public static Object interceptSuperCallable(@This Object mock,
return superCall.call();
}
return interceptor.doIntercept(
mock,
invokedMethod,
arguments,
new RealMethod.FromCallable(superCall)
);
mock,
invokedMethod,
arguments,
new RealMethod.FromCallable(superCall)
);
}

@SuppressWarnings("unused")
Expand All @@ -126,11 +124,11 @@ public static Object interceptAbstract(@This Object mock,
return stubValue;
}
return interceptor.doIntercept(
mock,
invokedMethod,
arguments,
RealMethod.IsIllegal.INSTANCE
);
mock,
invokedMethod,
arguments,
RealMethod.IsIllegal.INSTANCE
);
}
}
}
Expand Up @@ -98,7 +98,7 @@ public <T> Class<? extends T> mockClass(MockFeatures<T> features) {
if (localMock || loader instanceof MultipleParentClassLoader && !isComingFromJDK(features.mockedType)) {
typeName = features.mockedType.getName();
} else {
typeName = InjectionBase.class.getPackage().getName() + features.mockedType.getSimpleName();
typeName = InjectionBase.class.getPackage().getName() + "." + features.mockedType.getSimpleName();
}
String name = String.format("%s$%s$%d", typeName, "MockitoMock", Math.abs(random.nextInt()));

Expand Down

0 comments on commit 959cba4

Please sign in to comment.