Skip to content

Commit

Permalink
Fixes #832 Provide a new API for verifying static mock and deprecate …
Browse files Browse the repository at this point in the history
…old for Mockito

Signed-off-by: Arthur Zagretdinov <arthur.zagretdinov@outlook.com>
  • Loading branch information
Arthur Zagretdinov authored and thekingn0thing committed Aug 12, 2017
1 parent 4f34a08 commit 62c8fbb
Show file tree
Hide file tree
Showing 13 changed files with 445 additions and 527 deletions.
3 changes: 3 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Change log 1.7.1
* Fixed #832 Provide a new API for verifying static mock and deprecate old for Mockito

Change log 1.7.0
-----------------------------
* Added supporting Mockito 2.8.9 for PowerMock 2.x (thanks to Gregor Stamac @gstamac for pull request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,43 @@ public static synchronized <T> void spy(Class<T> type) {
* 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.
*
* @deprecated Will be removed in PowerMock 2. Please use {@link #verifyStatic(Class)}
*/
@Deprecated
public static synchronized void verifyStatic() {
verifyStatic(times(1));
}

/**
* Verifies certain behavior of the <code>mockedClass</code> <b>happened once</b>
* <p>
* Alias to {@code verifyStatic(classMock, times(1))} E.g:
*
* <pre>
* verifyStatic(ClassWithStaticMethod.class);
* ClassWithStaticMethod.someStaticMethod(&quot;some arg&quot;);
* </pre>
*
* Above is equivalent to:
*
* <pre>
* verifyStatic(ClassWithStaticMethod.class, times(1));
* ClassWithStaticMethod.someStaticMethod(&quot;some arg&quot;);
* </pre>
*
* <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.
*
* @param mockedClass the mocked class behavior of that have to be verified.
*/
public static synchronized <T> void verifyStatic(Class<T> mockedClass) {
verifyStatic(mockedClass, times(1));
}

/**
* Verifies certain behavior happened at least once / exact number of times
Expand All @@ -281,10 +314,41 @@ public static synchronized void verifyStatic() {
*
* @param verificationMode
* times(x), atLeastOnce() or never()
* @deprecated Will be removed in PowerMock 2. Please use {@link #verifyStatic(Class, VerificationMode)}
*/
@Deprecated
public static synchronized void verifyStatic(VerificationMode verificationMode) {
Whitebox.getInternalState(Mockito.class, MockingProgress.class).verificationStarted(
POWERMOCKITO_CORE.wrapInStaticVerificationMode(verificationMode));
POWERMOCKITO_CORE.wrapInStaticVerificationMode(verificationMode));
}

/**
* Verifies certain behavior of the <code>mockedClass</code> happened at least once / exact number of times
* / never. E.g:
*
* <pre>
* verifyStatic(ClassWithStaticMethod.class, times(5));
* ClassWithStaticMethod.someStaticMethod(&quot;was called five times&quot;);
*
* verifyStatic(ClassWithStaticMethod.class, atLeast(2));
* ClassWithStaticMethod.someStaticMethod(&quot;was called at least two times&quot;);
*
* //you can use flexible argument matchers, e.g:
* verifyStatic(ClassWithStaticMethod.class, atLeastOnce());
* ClassWithStaticMethod.someMethod(&lt;b&gt;anyString()&lt;/b&gt;);
* </pre>
*
* <b>times(1) is the default</b> and can be omitted
* <p>
*
* @param mockedClass the mocked class behavior of that have to be verified.
* @param verificationMode
* times(x), atLeastOnce() or never()
*
*/
public static synchronized <T> void verifyStatic(Class<T> mockedClass, VerificationMode verificationMode) {
Whitebox.getInternalState(Mockito.class, MockingProgress.class).verificationStarted(
POWERMOCKITO_CORE.wrapInStaticVerificationMode(mockedClass, verificationMode));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ private MockingProgress getMockingProgress() {
public MockAwareVerificationMode wrapInMockitoSpecificVerificationMode(Object mock, VerificationMode mode) {
return new MockAwareVerificationMode(mock, mode);
}

public <T> VerificationMode wrapInStaticVerificationMode(final Class<T> mockedClass, final VerificationMode verificationMode) {
return new StaticMockAwareVerificationMode(mockedClass, verificationMode);
}

public MockAwareVerificationMode wrapInStaticVerificationMode(VerificationMode mode) {
return new StaticMockAwareVerificationMode(mode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,32 @@
*/
public class StaticMockAwareVerificationMode extends MockAwareVerificationMode {

private Class<?> clsMock;
private Class<?> classMock;

public StaticMockAwareVerificationMode(VerificationMode mode) {
super(null, mode);
this(null, mode);
}


public <T> StaticMockAwareVerificationMode(final Class<T> classMock, final VerificationMode verificationMode) {
super(null, verificationMode);
this.classMock = classMock;
}

public void setClassMock(Class<?> clsMock) {
this.clsMock = clsMock;
this.classMock = clsMock;
}


public Class<?> getClassMock() {
return classMock;
}

@Override
public void verify(VerificationData data) {
super.verify(data);
}

@Override
public Object getMock() {
return clsMock;
return classMock;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.mockito.MockSettings;
import org.mockito.Mockito;
import org.mockito.internal.progress.MockingProgress;
import org.mockito.internal.progress.ThreadSafeMockingProgress;
import org.mockito.internal.stubbing.answers.CallsRealMethods;
import org.mockito.internal.stubbing.answers.DoesNothing;
Expand Down Expand Up @@ -241,10 +242,43 @@ public static synchronized <T> void spy(Class<T> type) {
* 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.
*
* @deprecated Will be removed in PowerMock 2. Please use {@link #verifyStatic(Class)}
*/
@Deprecated
public static synchronized void verifyStatic() {
verifyStatic(times(1));
}

/**
* Verifies certain behavior of the <code>mockedClass</code> <b>happened once</b>
* <p>
* Alias to {@code verifyStatic(classMock, times(1))} E.g:
*
* <pre>
* verifyStatic(ClassWithStaticMethod.class);
* ClassWithStaticMethod.someStaticMethod(&quot;some arg&quot;);
* </pre>
*
* Above is equivalent to:
*
* <pre>
* verifyStatic(ClassWithStaticMethod.class, times(1));
* ClassWithStaticMethod.someStaticMethod(&quot;some arg&quot;);
* </pre>
*
* <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.
*
* @param mockedClass the mocked class behavior of that have to be verified.
*/
public static synchronized <T> void verifyStatic(Class<T> mockedClass) {
verifyStatic(mockedClass, times(1));
}

/**
* Verifies certain behavior happened at least once / exact number of times
Expand All @@ -266,11 +300,43 @@ public static synchronized void verifyStatic() {
* <p>
*
* @param verificationMode times(x), atLeastOnce() or never()
*
* @deprecated Will be removed in PowerMock 2. Please use {@link #verifyStatic(Class, VerificationMode)}
*/
@Deprecated
public static synchronized void verifyStatic(VerificationMode verificationMode) {
ThreadSafeMockingProgress.mockingProgress().verificationStarted(
POWERMOCKITO_CORE.wrapInStaticVerificationMode(verificationMode));
}

/**
* Verifies certain behavior of the <code>mockedClass</code> happened at least once / exact number of times
* / never. E.g:
*
* <pre>
* verifyStatic(ClassWithStaticMethod.class, times(5));
* ClassWithStaticMethod.someStaticMethod(&quot;was called five times&quot;);
*
* verifyStatic(ClassWithStaticMethod.class, atLeast(2));
* ClassWithStaticMethod.someStaticMethod(&quot;was called at least two times&quot;);
*
* //you can use flexible argument matchers, e.g:
* verifyStatic(ClassWithStaticMethod.class, atLeastOnce());
* ClassWithStaticMethod.someMethod(&lt;b&gt;anyString()&lt;/b&gt;);
* </pre>
*
* <b>times(1) is the default</b> and can be omitted
* <p>
*
* @param mockedClass the mocked class behavior of that have to be verified.
* @param verificationMode
* times(x), atLeastOnce() or never()
*
*/
public static synchronized <T> void verifyStatic(Class<T> mockedClass, VerificationMode verificationMode) {
ThreadSafeMockingProgress.mockingProgress().verificationStarted(
POWERMOCKITO_CORE.wrapInStaticVerificationMode(mockedClass, verificationMode));
}

/**
* Verify a private method invocation for an instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,20 @@ public PowerMockitoStubber doAnswer(Answer answer) {
getMockingProgress().resetOngoingStubbing();
return (PowerMockitoStubber) new PowerMockitoStubberImpl().doAnswer(answer);
}

private MockingProgress getMockingProgress() {
return ThreadSafeMockingProgress.mockingProgress();
}

public MockAwareVerificationMode wrapInMockitoSpecificVerificationMode(Object mock, VerificationMode mode) {
return new MockAwareVerificationMode(mock, mode, getMockingProgress().verificationListeners());
}

public <T> VerificationMode wrapInStaticVerificationMode(final Class<T> mockedClass, final VerificationMode verificationMode) {
return new StaticMockAwareVerificationMode(mockedClass, verificationMode, getMockingProgress().verificationListeners());
}

public MockAwareVerificationMode wrapInStaticVerificationMode(VerificationMode mode) {
return new StaticMockAwareVerificationMode(mode, getMockingProgress().verificationListeners());
public MockAwareVerificationMode wrapInStaticVerificationMode(VerificationMode verificationMode) {
return new StaticMockAwareVerificationMode(verificationMode, getMockingProgress().verificationListeners());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ public Object invoke(final Object obj, final Method method, final Object[] argum
private void handleStaticVerification(Class<?> cls) {
VerificationMode verificationMode = getVerificationMode();
if (verificationMode instanceof StaticMockAwareVerificationMode) {
((StaticMockAwareVerificationMode) verificationMode).setClassMock(cls);
final StaticMockAwareVerificationMode mode = (StaticMockAwareVerificationMode) verificationMode;
if (mode.getClassMock() == null) {
mode.setClassMock(cls);
}
}
}

Expand Down Expand Up @@ -235,12 +238,12 @@ public Object invoke(Object target, Object[] arguments) throws Throwable {
});

Invocation invocation = new InvocationImpl(
interceptionObject,
new DelegatingMethod(method),
arguments,
SequenceNumber.next(),
cleanTraceRealMethod,
new LocationImpl()
interceptionObject,
new DelegatingMethod(method),
arguments,
SequenceNumber.next(),
cleanTraceRealMethod,
new LocationImpl()
);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,33 @@
*/
public class StaticMockAwareVerificationMode extends MockAwareVerificationMode {

private Class<?> clsMock;
private Class<?> classMock;

public StaticMockAwareVerificationMode(VerificationMode mode, Set<VerificationListener> listeners) {
this(null, mode, listeners);
}

public <T> StaticMockAwareVerificationMode(final Class<T> classMock, final VerificationMode mode,
final Set<VerificationListener> listeners) {
super(null, mode, listeners);
this.classMock = classMock;
}

public void setClassMock(Class<?> clsMock) {
this.clsMock = clsMock;
this.classMock = clsMock;
}


public Class<?> getClassMock() {
return classMock;
}

@Override
public void verify(VerificationData data) {
super.verify(data);
}

@Override
public Object getMock() {
return clsMock;
return classMock;
}
}

0 comments on commit 62c8fbb

Please sign in to comment.