Skip to content

Commit

Permalink
Fixes mockito#374 Removed deprecated types and methods (mockito#404)
Browse files Browse the repository at this point in the history
Removed deprecated DeprecatedOngoingStubbing and IOngoingStubbing
Removed deprecated method Mockito.stubVoid()
removed deprecated annotation MockitoAnnotations.Mock
Removed @deprecated annotations of inner classes of BDDMockito and made
them private
Removed deprecated methods VerificationWithTimeout.never() and
VerificationWithTimeout.atMost()
Removed deprecated class MockitoJUnitRule
Removed deprecated enum Answers
Removed deprecated method InvocationOnMock.getArgumentAt(int,Class)
Removed deprecated class ReturnsElementsOf
Fixed compile error in ExtraMatchers
corrected wrong package declaration in VerificationWithDescriptionTest
  • Loading branch information
ChristianSchwarz authored and TimvdLippe committed May 27, 2016
1 parent 8331e6e commit dff5510
Show file tree
Hide file tree
Showing 46 changed files with 121 additions and 826 deletions.
6 changes: 2 additions & 4 deletions src/main/java/org/mockito/Answers.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,5 @@ public enum Answers implements Answer<Object>{

public Object answer(InvocationOnMock invocation) throws Throwable {
return implementation.answer(invocation);
}


}
}
}
18 changes: 3 additions & 15 deletions src/main/java/org/mockito/BDDMockito.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,7 @@ public interface BDDMyOngoingStubbing<T> {
<M> M getMock();
}

/**
* @deprecated not part of the public API, use {@link BDDMyOngoingStubbing} instead.
*/
@Deprecated
public static class BDDOngoingStubbingImpl<T> implements BDDMyOngoingStubbing<T> {
private static class BDDOngoingStubbingImpl<T> implements BDDMyOngoingStubbing<T> {

private final OngoingStubbing<T> mockitoOngoingStubbing;

Expand Down Expand Up @@ -265,11 +261,7 @@ public interface Then<T> {
void shouldHaveNoMoreInteractions();
}

/**
* @deprecated not part of the public API, use {@link Then} instead.
*/
@Deprecated
static class ThenImpl<T> implements Then<T> {
private static class ThenImpl<T> implements Then<T> {

private final T mock;

Expand Down Expand Up @@ -394,11 +386,7 @@ public interface BDDStubber {
<T> T given(T mock);
}

/**
* @deprecated not part of the public API, use {@link BDDStubber} instead.
*/
@Deprecated
public static class BDDStubberImpl implements BDDStubber {
private static class BDDStubberImpl implements BDDStubber {

private final Stubber mockitoStubber;

Expand Down
100 changes: 2 additions & 98 deletions src/main/java/org/mockito/Mockito.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,6 @@
*
* Read more about doThrow|doAnswer family of methods in paragraph 12.
* <p>
* Initially, {@link Mockito#stubVoid(Object)} was used for stubbing voids.
* Currently <code>stubVoid()</code> is deprecated in favor of {@link Mockito#doThrow(Throwable...)}.
* This is because of improved readability and consistency with the family of {@link Mockito#doAnswer(Answer)} methods.
*
*
*
*
* <h3 id="6">6. <a class="meaningful_link" href="#in_order_verification">Verification in order</a></h3>
*
Expand Down Expand Up @@ -1568,65 +1562,11 @@ public static <T> T spy(Class<T> classToSpy) {
.defaultAnswer(CALLS_REAL_METHODS));
}

/**
* Stubs a method call with return value or an exception. E.g:
*
* <pre class="code"><code class="java">
* stub(mock.someMethod()).toReturn(10);
*
* //you can use flexible argument matchers, e.g:
* stub(mock.someMethod(<b>anyString()</b>)).toReturn(10);
*
* //setting exception to be thrown:
* stub(mock.someMethod("some arg")).toThrow(new RuntimeException());
*
* //you can stub with different behavior for consecutive method calls.
* //Last stubbing (e.g: toReturn("foo")) determines the behavior for further consecutive calls.
* stub(mock.someMethod("some arg"))
* .toThrow(new RuntimeException())
* .toReturn("foo");
* </code></pre>
* <p>
* Some users find stub() confusing therefore {@link Mockito#when(Object)} is recommended over stub()
* <pre class="code"><code class="java">
* //Instead of:
* stub(mock.count()).toReturn(10);
*
* //You can do:
* when(mock.count()).thenReturn(10);
* </code></pre>
* For stubbing void methods with throwables see: {@link Mockito#doThrow(Throwable...)}
* <p>
* Stubbing can be overridden: for example common stubbing can go to fixture
* setup but the test methods can override it.
* Please note that overridding stubbing is a potential code smell that points out too much stubbing.
* <p>
* Once stubbed, the method will always return stubbed value regardless
* of how many times it is called.
* <p>
* Last stubbing is more important - when you stubbed the same method with
* the same arguments many times.
* <p>
* Although it is possible to verify a stubbed invocation, usually <b>it's just redundant</b>.
* Let's say you've stubbed foo.bar().
* If your code cares what foo.bar() returns then something else breaks(often before even 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>.
*
* @param methodCall
* method call
* @return DeprecatedOngoingStubbing object to set stubbed value/exception
*/
public static <T> DeprecatedOngoingStubbing<T> stub(T methodCall) {
return MOCKITO_CORE.stub(methodCall);
}

/**
* Enables stubbing methods. Use it when you want the mock to return particular value when particular method is called.
* <p>
* Simply put: "<b>When</b> the x method is called <b>then</b> return y".
* <p>
* <b>when() is a successor of deprecated {@link Mockito#stub(Object)}</b>
*
* <p>
* Examples:
*
Expand Down Expand Up @@ -1854,42 +1794,6 @@ public static void verifyZeroInteractions(Object... mocks) {
MOCKITO_CORE.verifyNoMoreInteractions(mocks);
}

/**
* <pre class="code"><code class="java">
* //Instead of:
* stubVoid(mock).toThrow(e).on().someVoidMethod();
*
* //Please do:
* doThrow(e).when(mock).someVoidMethod();
* </code></pre>
*
* doThrow() replaces stubVoid() because of improved readability and consistency with the family of doAnswer() methods.
* <p>
* Originally, <code>stubVoid()</code> was used for stubbing void methods with exceptions. E.g:
*
* <pre class="code"><code class="java">
* stubVoid(mock).toThrow(new RuntimeException()).on().someMethod();
*
* //you can stub with different behavior for consecutive calls.
* //Last stubbing (e.g. toReturn()) determines the behavior for further consecutive calls.
* stubVoid(mock)
* .toThrow(new RuntimeException())
* .toReturn()
* .on().someMethod();
* </code></pre>
*
* See examples in javadoc for {@link Mockito} class
*
* @deprecated Use {@link Mockito#doThrow(Throwable...)} method for stubbing voids
*
* @param mock
* to stub
* @return stubbable object that allows stubbing with throwable
*/
public static <T> VoidMethodStubbable<T> stubVoid(T mock) {
return MOCKITO_CORE.stubVoid(mock);
}

/**
* Use <code>doThrow()</code> when you want to stub the void method with an exception.
* <p>
Expand Down Expand Up @@ -2568,4 +2472,4 @@ public static VerificationMode description(String description) {
static MockitoDebugger debug() {
return new MockitoDebuggerImpl();
}
}
}
18 changes: 0 additions & 18 deletions src/main/java/org/mockito/MockitoAnnotations.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,6 @@
*/
public class MockitoAnnotations {

/**
* Use top-level {@link org.mockito.Mock} annotation instead
* <p>
* When &#064;Mock annotation was implemented as an inner class then users experienced problems with autocomplete features in IDEs.
* Hence &#064;Mock was made a top-level class.
* <p>
* How to fix deprecation warnings?
* Typically, you can just <b>search:</b> import org.mockito.MockitoAnnotations.Mock; <b>and replace with:</b> import org.mockito.Mock;
* <p>
* If you're an existing user then sorry for making your code littered with deprecation warnings.
* This change was required to make Mockito better.
*/
@Target( { FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Deprecated
public @interface Mock {}

/**
* Initializes objects annotated with Mockito annotations for given testClass:
* &#064;{@link org.mockito.Mock}, &#064;{@link Spy}, &#064;{@link Captor}, &#064;{@link InjectMocks}
Expand All @@ -103,7 +86,6 @@ public static void initMocks(Object testClass) {
clazz = clazz.getSuperclass();
}
}

//anyway act 'the new' way
annotationEngine.process(testClass.getClass(), testClass);
}
Expand Down
18 changes: 4 additions & 14 deletions src/main/java/org/mockito/internal/MockitoCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.mockito.exceptions.misusing.NotAMockException;
import org.mockito.internal.creation.MockSettingsImpl;
import org.mockito.internal.invocation.finder.VerifiableInvocationsFinder;
import org.mockito.internal.progress.IOngoingStubbing;
import org.mockito.internal.progress.MockingProgress;
import org.mockito.internal.progress.ThreadSafeMockingProgress;
import org.mockito.internal.stubbing.InvocationContainer;
Expand Down Expand Up @@ -56,25 +55,16 @@ public <T> T mock(Class<T> typeToMock, MockSettings settings) {
mockingProgress.mockingStarted(mock, typeToMock);
return mock;
}

public IOngoingStubbing stub() {
IOngoingStubbing stubbing = mockingProgress.pullOngoingStubbing();

public <T> OngoingStubbing<T> when(T methodCall) {
mockingProgress.stubbingStarted();
OngoingStubbing<T> stubbing = mockingProgress.pullOngoingStubbing();
if (stubbing == null) {
mockingProgress.reset();
reporter.missingMethodInvocation();
}
return stubbing;
}

public <T> DeprecatedOngoingStubbing<T> stub(T methodCall) {
mockingProgress.stubbingStarted();
return (DeprecatedOngoingStubbing) stub();
}

public <T> OngoingStubbing<T> when(T methodCall) {
mockingProgress.stubbingStarted();
return (OngoingStubbing) stub();
}

public <T> T verify(T mock, VerificationMode mode) {
if (mock == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@ public class DefaultAnnotationEngine implements AnnotationEngine {

public DefaultAnnotationEngine() {
registerAnnotationProcessor(Mock.class, new MockAnnotationProcessor());
registerAnnotationProcessor(MockitoAnnotations.Mock.class, new MockitoAnnotationsMockAnnotationProcessor());
registerAnnotationProcessor(Captor.class, new CaptorAnnotationProcessor());
}

/* (non-Javadoc)
* @see org.mockito.AnnotationEngine#createMockFor(java.lang.annotation.Annotation, java.lang.reflect.Field)
*/
@SuppressWarnings("deprecation")
public Object createMockFor(Annotation annotation, Field field) {
return forAnnotation(annotation).process(annotation, field);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ public Object createMockFor(Annotation annotation, Field field) {
return null;
}

@SuppressWarnings("deprecation") // for MockitoAnnotations.Mock
public void process(Class<?> context, Object testInstance) {
Field[] fields = context.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(Spy.class) && !field.isAnnotationPresent(InjectMocks.class)) {
assertNoIncompatibleAnnotations(Spy.class, field, Mock.class, org.mockito.MockitoAnnotations.Mock.class, Captor.class);
assertNoIncompatibleAnnotations(Spy.class, field, Mock.class, Captor.class);
field.setAccessible(true);
Object instance;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private Set<Field> scan() {
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (null != field.getAnnotation(InjectMocks.class)) {
assertNoAnnotations(field, Mock.class, MockitoAnnotations.Mock.class, Captor.class);
assertNoAnnotations(field, Mock.class, Captor.class);
mockDependentFields.add(field);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package org.mockito.internal.configuration.injection.scanner;

import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.mockito.internal.util.MockUtil;
import org.mockito.internal.util.reflection.FieldReader;
Expand Down Expand Up @@ -69,17 +68,16 @@ private Set<Object> scan() {
private Object preparedMock(Object instance, Field field) {
if (isAnnotatedByMockOrSpy(field)) {
return instance;
} else if (isMockOrSpy(instance)) {
}
if (isMockOrSpy(instance)) {
mockUtil.maybeRedefineMockName(instance, field.getName());
return instance;
}
return null;
}

private boolean isAnnotatedByMockOrSpy(Field field) {
return null != field.getAnnotation(Spy.class)
|| null != field.getAnnotation(Mock.class)
|| null != field.getAnnotation(MockitoAnnotations.Mock.class);
return field.isAnnotationPresent(Spy.class) || field.isAnnotationPresent(Mock.class);
}

private boolean isMockOrSpy(Object instance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,6 @@ public Object[] getArguments() {
return arguments;
}

@Override
@Deprecated
public <T> T getArgumentAt(int index, Class<T> clazz) {
return (T) getArgument(index);
}

@Override
public <T> T getArgument(int index) {
return (T)arguments[index];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ public Object[] getArguments() {
return arguments;
}

@Deprecated
public <T> T getArgumentAt(int index, Class<T> clazz) {
return (T) getArgument(index);
}

public <T> T getArgument(int index) {
return (T)arguments[index];
}
Expand Down
Loading

0 comments on commit dff5510

Please sign in to comment.