Skip to content

Commit

Permalink
Merge branch 'release/3.x' of https://github.com/mockito/mockito into…
Browse files Browse the repository at this point in the history
… mockito-release/3.x
  • Loading branch information
drodriguezhdez committed Nov 25, 2019
2 parents 416086e + d6cec39 commit 1eeda83
Show file tree
Hide file tree
Showing 29 changed files with 412 additions and 192 deletions.
1 change: 1 addition & 0 deletions build.gradle
Expand Up @@ -133,6 +133,7 @@ spotless {
removeUnusedImports()
trimTrailingWhitespace()
endWithNewline()
indentWithSpaces(4)

importOrder 'static java', 'static javax', 'static ', 'java', 'javax', ''
}
Expand Down
27 changes: 27 additions & 0 deletions doc/release-notes/official.md
@@ -1,5 +1,32 @@
<sup><sup>*Release notes were automatically generated by [Shipkit](http://shipkit.org/)*</sup></sup>

#### 3.1.12
- 2019-11-20 - [1 commit](https://github.com/mockito/mockito/compare/v3.1.11...v3.1.12) by [Tim van der Lippe](https://github.com/TimvdLippe) - published to [![Bintray](https://img.shields.io/badge/Bintray-3.1.12-green.svg)](https://bintray.com/mockito/maven/mockito-development/3.1.12)
- Deprecate MockitoHamcrest [(#1819)](https://github.com/mockito/mockito/pull/1819)

#### 3.1.11
- 2019-11-18 - [1 commit](https://github.com/mockito/mockito/compare/v3.1.10...v3.1.11) by [Tim van der Lippe](https://github.com/TimvdLippe) - published to [![Bintray](https://img.shields.io/badge/Bintray-3.1.11-green.svg)](https://bintray.com/mockito/maven/mockito-development/3.1.11)
- Deprecate MockitoHamcrest [(#1819)](https://github.com/mockito/mockito/pull/1819)

#### 3.1.10
- 2019-11-13 - [2 commits](https://github.com/mockito/mockito/compare/v3.1.9...v3.1.10) by [Brice Dutheil](https://github.com/bric3) (1), [Serż Kwiatkowski](https://github.com/scadgek) (1) - published to [![Bintray](https://img.shields.io/badge/Bintray-3.1.10-green.svg)](https://bintray.com/mockito/maven/mockito-development/3.1.10)
- fix a word mistake [(#1823)](https://github.com/mockito/mockito/pull/1823)

#### 3.1.9
- 2019-11-10 - [1 commit](https://github.com/mockito/mockito/compare/v3.1.8...v3.1.9) by [Marc Philipp](https://github.com/marcphilipp) - published to [![Bintray](https://img.shields.io/badge/Bintray-3.1.9-green.svg)](https://bintray.com/mockito/maven/mockito-development/3.1.9)
- Guard against multiple evaluations of before statement [(#1821)](https://github.com/mockito/mockito/pull/1821)
- 'MismatchReportingTestListener' has already been added and not removed using MockitoJUnitRunner [(#1767)](https://github.com/mockito/mockito/issues/1767)
- Report initialization failures per test method [(#1672)](https://github.com/mockito/mockito/pull/1672)

#### 3.1.8
- 2019-11-07 - [1 commit](https://github.com/mockito/mockito/compare/v3.1.7...v3.1.8) by [Tim van der Lippe](https://github.com/TimvdLippe) - published to [![Bintray](https://img.shields.io/badge/Bintray-3.1.8-green.svg)](https://bintray.com/mockito/maven/mockito-development/3.1.8)
- Indent with spaces using spotless [(#1820)](https://github.com/mockito/mockito/pull/1820)

#### 3.1.7
- 2019-11-07 - [1 commit](https://github.com/mockito/mockito/compare/v3.1.6...v3.1.7) by [Kurt Alfred Kluever](https://github.com/kluever) - published to [![Bintray](https://img.shields.io/badge/Bintray-3.1.7-green.svg)](https://bintray.com/mockito/maven/mockito-development/3.1.7)
- Add Duration overloads. Fixes #1815 [(#1818)](https://github.com/mockito/mockito/pull/1818)
- FR: add Mockito.timeout(java.time.Duration) and after(java.time.Duration) [(#1815)](https://github.com/mockito/mockito/issues/1815)

#### 3.1.6
- 2019-11-06 - [1 commit](https://github.com/mockito/mockito/compare/v3.1.5...v3.1.6) by [diguage](https://github.com/diguage) - published to [![Bintray](https://img.shields.io/badge/Bintray-3.1.6-green.svg)](https://bintray.com/mockito/maven/mockito-development/3.1.6)
- fix document error [(#1816)](https://github.com/mockito/mockito/pull/1816)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/mockito/InjectMocks.java
Expand Up @@ -39,7 +39,7 @@
* then, if there is several property of the same type, by the match of the property name and the mock name.
* <p><u>Note 1:</u> If you have properties with the same type (or same erasure), it's better to name all &#064;Mock
* annotated fields with the matching properties, otherwise Mockito might get confused and injection won't happen.</p>
* <p><u>Note 2:</u> If &#064;InjectMocks instance wasn't initialized before and have a no-arg constructor,
* <p><u>Note 2:</u> If &#064;InjectMocks instance wasn't initialized before and has a no-arg constructor,
* then it will be initialized with this constructor.</p></li>
*
* <li><strong>Field injection</strong>; mocks will first be resolved by type (if a single type match
Expand Down
109 changes: 102 additions & 7 deletions src/main/java/org/mockito/Mockito.java
Expand Up @@ -4,6 +4,8 @@
*/
package org.mockito;

import java.time.Duration;

import org.mockito.exceptions.misusing.PotentialStubbingProblem;
import org.mockito.exceptions.misusing.UnnecessaryStubbingException;
import org.mockito.internal.InternalMockHandler;
Expand Down Expand Up @@ -886,15 +888,15 @@
* <pre class="code"><code class="java">
* //passes when someMethod() is called no later than within 100 ms
* //exits immediately when verification is satisfied (e.g. may not wait full 100 ms)
* verify(mock, timeout(100)).someMethod();
* verify(mock, timeout(Duration.ofMillis(100))).someMethod();
* //above is an alias to:
* verify(mock, timeout(100).times(1)).someMethod();
* verify(mock, timeout(Duration.ofMillis(100)).times(1)).someMethod();
*
* //passes as soon as someMethod() has been called 2 times under 100 ms
* verify(mock, timeout(100).times(2)).someMethod();
* verify(mock, timeout(Duration.ofMillis(100)).times(2)).someMethod();
*
* //equivalent: this also passes as soon as someMethod() has been called 2 times under 100 ms
* verify(mock, timeout(100).atLeast(2)).someMethod();
* verify(mock, timeout(Duration.ofMillis(100)).atLeast(2)).someMethod();
* </code></pre>
*
*
Expand Down Expand Up @@ -2846,10 +2848,45 @@ public static VerificationMode only() {
* @param millis - duration in milliseconds
*
* @return object that allows fluent specification of the verification (times(x), atLeast(y), etc.)
* @deprecated Use {@link #timeout(Duration)} instead.
*/
@CheckReturnValue
@Deprecated
public static VerificationWithTimeout timeout(long millis) {
return new Timeout(millis, VerificationModeFactory.times(1));
return timeout(Duration.ofMillis(millis));
}

/**
* Verification will be triggered over and over until the given amount of time, allowing testing of async code.
* Useful when interactions with the mock object did not happened yet.
* Extensive use of {@code timeout()} method can be a code smell - there are better ways of testing concurrent code.
* <p>
* See also {@link #after(Duration)} method for testing async code.
* Differences between {@code timeout()} and {@code after} are explained in Javadoc for {@link #after(Duration)}.
*
* <pre class="code"><code class="java">
* //passes when someMethod() is called no later than within 100 ms
* //exits immediately when verification is satisfied (e.g. may not wait full 100 ms)
* verify(mock, timeout(Duration.ofMillis(100))).someMethod();
* //above is an alias to:
* verify(mock, timeout(Duration.ofMillis(100)).times(1)).someMethod();
*
* //passes as soon as someMethod() has been called 2 times under 100 ms
* verify(mock, timeout(Duration.ofMillis(100)).times(2)).someMethod();
*
* //equivalent: this also passes as soon as someMethod() has been called 2 times under 100 ms
* verify(mock, timeout(Duration.ofMillis(100)).atLeast(2)).someMethod();
* </code></pre>
*
* See examples in javadoc for {@link Mockito} class
*
* @param timeout how long to wait before timing out
*
* @return object that allows fluent specification of the verification (times(x), atLeast(y), etc.)
*/
@CheckReturnValue
public static VerificationWithTimeout timeout(Duration timeout) {
return new Timeout(timeout, VerificationModeFactory.times(1));
}

/**
Expand Down Expand Up @@ -2889,7 +2926,7 @@ public static VerificationWithTimeout timeout(long millis) {
* //1.
* mock.foo();
* verify(mock, after(1000)).foo();
* //waits 1000 millis and succeeds
* //waits 1 second and succeeds
*
* //2.
* mock.foo();
Expand All @@ -2902,10 +2939,68 @@ public static VerificationWithTimeout timeout(long millis) {
* @param millis - duration in milliseconds
*
* @return object that allows fluent specification of the verification
* @deprecated Use {@link #after(Duration)} instead.
*/
@CheckReturnValue
@Deprecated
public static VerificationAfterDelay after(long millis) {
return new After(millis, VerificationModeFactory.times(1));
return after(Duration.ofMillis(millis));
}

/**
* Verification will be triggered after given amount of time, allowing testing of async code.
* Useful when interactions with the mock object did not happened yet.
* Extensive use of {@code after()} method can be a code smell - there are better ways of testing concurrent code.
* <p>
* Not yet implemented to work with InOrder verification.
* <p>
* See also {@link #timeout(Duration)} method for testing async code.
* Differences between {@code timeout()} and {@code after()} are explained below.
*
* <pre class="code"><code class="java">
* //passes after 100ms, if someMethod() has only been called once at that time.
* verify(mock, after(Duration.ofMillis(100))).someMethod();
* //above is an alias to:
* verify(mock, after(Duration.ofMillis(100)).times(1)).someMethod();
*
* //passes if someMethod() is called <b>*exactly*</b> 2 times, as tested after 100 millis
* verify(mock, after(Duration.ofMillis(100)).times(2)).someMethod();
*
* //passes if someMethod() has not been called, as tested after 100 millis
* verify(mock, after(Duration.ofMillis(100)).never()).someMethod();
*
* //verifies someMethod() after a given time span using given verification mode
* //useful only if you have your own custom verification modes.
* verify(mock, new After(Duration.ofMillis(100), yourOwnVerificationMode)).someMethod();
* </code></pre>
*
* <strong>timeout() vs. after()</strong>
* <ul>
* <li>timeout() exits immediately with success when verification passes</li>
* <li>after() awaits full duration to check if verification passes</li>
* </ul>
* Examples:
* <pre class="code"><code class="java">
* //1.
* mock.foo();
* verify(mock, after(Duration.ofSeconds(1))).foo();
* //waits 1 second and succeeds
*
* //2.
* mock.foo();
* verify(mock, timeout(Duration.ofSeconds(1))).foo();
* //succeeds immediately
* </code></pre>
*
* See examples in javadoc for {@link Mockito} class
*
* @param delay how to to wait before triggering verification
*
* @return object that allows fluent specification of the verification
*/
@CheckReturnValue
public static VerificationAfterDelay after(Duration delay) {
return new After(delay, VerificationModeFactory.times(1));
}

/**
Expand Down
Expand Up @@ -40,10 +40,10 @@ protected boolean processInjection(Field field, Object fieldOwner, Set<Object> m
Mockito.reset(instance);
} else {
Object mock = Mockito.mock(instance.getClass(), withSettings()
.spiedInstance(instance)
.defaultAnswer(Mockito.CALLS_REAL_METHODS)
.name(field.getName()));
setField(fieldOwner, field, mock);
.spiedInstance(instance)
.defaultAnswer(Mockito.CALLS_REAL_METHODS)
.name(field.getName()));
setField(fieldOwner, field, mock);
}
} catch (Exception e) {
throw new MockitoException("Problems initiating spied field " + field.getName(), e);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/mockito/internal/exceptions/Reporter.java
Expand Up @@ -10,6 +10,7 @@

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -863,12 +864,12 @@ public static MockitoException usingConstructorWithFancySerializable(Serializabl
return new MockitoException("Mocks instantiated with constructor cannot be combined with " + mode + " serialization mode.");
}

public static MockitoException cannotCreateTimerWithNegativeDurationTime(long durationMillis) {
public static MockitoException cannotCreateTimerWithNegativeDurationTime(Duration duration) {
return new FriendlyReminderException(join(
"",
"Don't panic! I'm just a friendly reminder!",
"It is impossible for time to go backward, therefore...",
"You cannot put negative value of duration: (" + durationMillis + ")",
"You cannot put negative value of duration: (" + duration + ")",
"as argument of timer methods (after(), timeout())",
""
));
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/mockito/internal/junit/JUnitRule.java
Expand Up @@ -31,8 +31,8 @@ public JUnitRule(MockitoLogger logger, Strictness strictness) {
this.strictness = strictness;
}

@Override
public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
@Override
public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
return new Statement() {
public void evaluate() throws Throwable {
if (session == null) {
Expand Down
Expand Up @@ -36,11 +36,13 @@ protected Statement withBefores(FrameworkMethod method, final Object target, Sta
return new Statement() {
@Override
public void evaluate() throws Throwable {
// get new test listener and add it to the framework
mockitoTestListener = listenerSupplier.get();
Mockito.framework().addListener(mockitoTestListener);
// init annotated mocks before tests
MockitoAnnotations.initMocks(target);
if (mockitoTestListener == null) {
// get new test listener and add it to the framework
mockitoTestListener = listenerSupplier.get();
Mockito.framework().addListener(mockitoTestListener);
// init annotated mocks before tests
MockitoAnnotations.initMocks(target);
}
base.evaluate();
}
};
Expand All @@ -61,6 +63,7 @@ public void testFinished(Description description) throws Exception {
if (mockitoTestListener != null) {
Mockito.framework().removeListener(mockitoTestListener);
mockitoTestListener.testFinished(new DefaultTestFinishedEvent(target, description.getMethodName(), failure));
mockitoTestListener = null;
}
Mockito.validateMockitoUsage();
} catch (Throwable t) {
Expand Down
Expand Up @@ -25,7 +25,7 @@
* @since 2.1.0
*/
public class AnswerFunctionalInterfaces {
/**
/**
* Hide constructor to avoid instantiation of class with only static methods
*/
private AnswerFunctionalInterfaces() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/mockito/internal/util/MockUtil.java
Expand Up @@ -89,7 +89,7 @@ public static void maybeRedefineMockName(Object mock, String newName) {
MockName mockName = getMockName(mock);
//TODO SF hacky...
MockCreationSettings mockSettings = getMockHandler(mock).getMockSettings();
if (mockName.isDefault() && mockSettings instanceof CreationSettings) {
if (mockName.isDefault() && mockSettings instanceof CreationSettings) {
((CreationSettings) mockSettings).setMockName(new MockNameImpl(newName));
}
}
Expand Down
30 changes: 17 additions & 13 deletions src/main/java/org/mockito/internal/util/Timer.java
Expand Up @@ -6,38 +6,42 @@

import static org.mockito.internal.exceptions.Reporter.cannotCreateTimerWithNegativeDurationTime;

import java.time.Duration;
import java.time.Instant;

public class Timer {

private final long durationMillis;
private long startTime = -1;
private final Duration duration;
private Instant startTime = null;

public Timer(long durationMillis) {
validateInput(durationMillis);
this.durationMillis = durationMillis;
public Timer(Duration duration) {
validateInput(duration);
this.duration = duration;
}

/**
* Informs whether the timer is still counting down.
*/
public boolean isCounting() {
assert startTime != -1;
return System.currentTimeMillis() - startTime <= durationMillis;
assert startTime != null;
Duration elapsed = Duration.between(startTime, Instant.now());
return elapsed.compareTo(duration) <= 0;
}

/**
* Starts the timer count down.
*/
public void start() {
startTime = System.currentTimeMillis();
startTime = Instant.now();
}

private void validateInput(long durationMillis) {
if (durationMillis < 0) {
throw cannotCreateTimerWithNegativeDurationTime(durationMillis);
private void validateInput(Duration duration) {
if (duration.isNegative()) {
throw cannotCreateTimerWithNegativeDurationTime(duration);
}
}

public long duration() {
return durationMillis;
public Duration duration() {
return duration;
}
}

0 comments on commit 1eeda83

Please sign in to comment.