Permalink
Browse files

Support Mockito 2.4.0 for PowerMock 1.x (#740)

* Support Mockito 2.4.0 for PowerMock 1.x (#737)
1 parent 8552c11 commit a89e8e5760f57c6d1b92b9049a44d43da92150da @thekingnothing thekingnothing committed on GitHub Jan 25, 2017
Showing with 785 additions and 553 deletions.
  1. +1 −1 build.gradle
  2. +1 −0 changelog.txt
  3. +8 −6 ...main/java/org/powermock/api/mockito/internal/expectation/AbstractConstructorExpectationSetup.java
  4. +0 −2 ...mmon/src/main/java/org/powermock/api/mockito/internal/invocation/MockitoNewInvocationControl.java
  5. 0 ...kito}/src/main/java/org/powermock/api/extension/reporter/MockingFrameworkReporterFactoryImpl.java
  6. +13 −1 .../main/java/org/powermock/api/mockito/internal/expectation/DefaultConstructorExpectationSetup.java
  7. +1 −1 .../powermock-api-mockito2/src/main/java/org/powermock/api/extension/listener/AnnotationEnabler.java
  8. +88 −0 ...kito2/src/main/java/org/powermock/api/extension/reporter/MockingFrameworkReporterFactoryImpl.java
  9. +10 −4 powermock-api/powermock-api-mockito2/src/main/java/org/powermock/api/mockito/PowerMockito.java
  10. +6 −3 ...api/powermock-api-mockito2/src/main/java/org/powermock/api/mockito/internal/PowerMockitoCore.java
  11. +1 −1 ...c/main/java/org/powermock/api/mockito/internal/configuration/PowerMockitoSpyAnnotationEngine.java
  12. +2 −2 ...ckito2/src/main/java/org/powermock/api/mockito/internal/exceptions/StackTraceCleanerProvider.java
  13. +13 −0 .../main/java/org/powermock/api/mockito/internal/expectation/DefaultConstructorExpectationSetup.java
  14. +6 −10 ...2/src/main/java/org/powermock/api/mockito/internal/invocation/MockitoMethodInvocationControl.java
  15. +2 −2 ...mockito2/src/main/java/org/powermock/api/mockito/internal/invocation/PowerMockMatchersBinder.java
  16. +1 −1 ...k-api-mockito2/src/main/java/org/powermock/api/mockito/internal/invocation/ToStringGenerator.java
  17. +192 −189 ...pi-mockito2/src/main/java/org/powermock/api/mockito/internal/mockcreation/DefaultMockCreator.java
  18. +3 −1 ...i-mockito2/src/main/java/org/powermock/api/mockito/internal/mockcreation/MockitoStateCleaner.java
  19. +6 −1 ...c/main/java/org/powermock/api/mockito/internal/mockcreation/PowerMockMethodInterceptorFilter.java
  20. +8 −0 ...mockito2/src/main/java/org/powermock/api/mockito/internal/mockcreation/RuntimeExceptionProxy.java
  21. +1 −7 ...rmock-api-mockito2/src/main/java/org/powermock/api/mockito/internal/mockmaker/PowerMockMaker.java
  22. +5 −2 ...rc/main/java/org/powermock/api/mockito/internal/verification/StaticMockAwareVerificationMode.java
  23. +29 −30 ...pi-mockito2/src/main/java/org/powermock/api/mockito/repackaged/AcrossJVMSerializationFeature.java
  24. +0 −1 .../powermock-api-mockito2/src/main/java/org/powermock/api/mockito/repackaged/ClassImposterizer.java
  25. +9 −2 ...api-mockito2/src/main/java/org/powermock/api/mockito/repackaged/DelegatingMockitoMethodProxy.java
  26. +16 −6 ...mock-api-mockito2/src/main/java/org/powermock/api/mockito/repackaged/MethodInterceptorFilter.java
  27. +79 −0 ...wermock-api-mockito2/src/main/java/org/powermock/api/mockito/repackaged/SearchingClassLoader.java
  28. +49 −43 ...i-mockito2/src/main/java/org/powermock/api/mockito/repackaged/SerializableMockitoMethodProxy.java
  29. +2 −2 ...-api-mockito2/src/main/java/org/powermock/api/mockito/repackaged/asm/util/CheckMethodAdapter.java
  30. +2 −4 ...k-api-mockito2/src/test/java/org/powermock/api/mockito/internal/mockcreation/MockCreatorTest.java
  31. +1 −1 ...-mockito2/src/test/java/org/powermock/api/mockito/internal/stubbing/answers/ChainReturnsTest.java
  32. +56 −56 ...ito-junit4/src/test/java/org/powermock/modules/junit4/largemethod/LargeMethodInInterfaceTest.java
  33. +58 −58 tests/mockito/junit4-agent/src/test/java/samples/powermockito/junit4/agent/LargeMethodTest.java
  34. +1 −1 ...o/junit4-agent/src/test/java/samples/powermockito/junit4/agent/MockFinalUsingAnnotationsTest.java
  35. +1 −1 ...a/powermock/modules/test/mockito/junit4/delegate/parameterized/MockFinalUsingAnnotationsTest.java
  36. +1 −1 ...nit4/src/test/java/samples/powermockito/junit4/annotationbased/MockFinalUsingAnnotationsTest.java
  37. +0 −2 tests/mockito/junit4/src/test/java/samples/powermockito/junit4/bugs/github701/GitHub701Test.java
  38. +56 −56 tests/mockito/junit4/src/test/java/samples/powermockito/junit4/largemethod/LargeMethodTest.java
  39. +3 −0 ...ito/junit4/src/test/java/samples/powermockito/junit4/staticmocking/MockStaticNotPreparedTest.java
  40. +31 −31 ...ito/testng/src/test/java/samples/powermockito/testng/staticmocking/MockStaticNotPreparedTest.java
  41. +23 −24 .../test/java/samples/powermockito/testng/staticmocking/MockStaticPreparedWithoutMockStaticTest.java
View
@@ -31,7 +31,7 @@ ext{
testngVersion = "6.9.10"
xstreamVersion = "1.4.9"
mockito1Version = "1.10.19"
- mockito2Version = "2.0.42-beta"
+ mockito2Version = "2.4.0"
servletVersion = "2.5"
systemRulesVersion = "1.16.0"
jacocoVersion = "0.7.7.201606060606"
View
@@ -3,6 +3,7 @@ Change log 1.7.0
* Fix Issue #722 IllegalArgumentException is output to standard error (thanks to Kotaro Nanri @k-nanri for pull request)
* #724 Optimizations in powermock-reflect (thanks to Roman Leventov @leventov for pull request)
* #728 Migrate from Maven to Gradle
+* Support Mockito 2.4.0 for PowerMock 1.x (thanks to Gregor Stamac @gstamac for pull request)
Change log 1.6.6 (2016-11-04)
-----------------------------
@@ -1,10 +1,8 @@
package org.powermock.api.mockito.internal.expectation;
-import org.mockito.Matchers;
import org.mockito.stubbing.OngoingStubbing;
import org.powermock.api.mockito.expectation.ConstructorExpectationSetup;
import org.powermock.api.mockito.expectation.WithExpectedArguments;
-import org.powermock.api.mockito.internal.invocation.MockitoNewInvocationControl;
import org.powermock.api.mockito.internal.mockcreation.MockCreator;
import org.powermock.core.MockRepository;
import org.powermock.core.spi.NewInvocationControl;
@@ -15,6 +13,7 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
public abstract class AbstractConstructorExpectationSetup<T> implements ConstructorExpectationSetup<T> {
@@ -44,11 +43,11 @@ public AbstractConstructorExpectationSetup(Class<T> mockType) {
/*
* Check if this type has been mocked before
*/
- NewInvocationControl<OngoingStubbing<T>> newInvocationControl = (NewInvocationControl<OngoingStubbing<T>>) MockRepository
- .getNewInstanceControl(unmockedType);
+ NewInvocationControl<OngoingStubbing<T>> newInvocationControl =
+ (NewInvocationControl<OngoingStubbing<T>>) MockRepository.getNewInstanceControl(unmockedType);
if (newInvocationControl == null) {
InvocationSubstitute<T> mock = getMockCreator().createMock(InvocationSubstitute.class, false, false, null, null, (Method[]) null);
- newInvocationControl = new MockitoNewInvocationControl(mock);
+ newInvocationControl = createNewInvocationControl(mock);
MockRepository.putNewInstanceControl(type, newInvocationControl);
MockRepository.addObjectsToAutomaticallyReplayAndVerify(WhiteboxImpl.getUnmockedType(type));
}
@@ -57,6 +56,7 @@ public AbstractConstructorExpectationSetup(Class<T> mockType) {
}
abstract MockCreator getMockCreator();
+ abstract <T> NewInvocationControl<OngoingStubbing<T>> createNewInvocationControl(InvocationSubstitute<T> mock);
void setParameterTypes(Class<?>[] parameterTypes) {
this.parameterTypes = parameterTypes;
@@ -84,14 +84,16 @@ void setParameterTypes(Class<?>[] parameterTypes) {
Object[] paramArgs = new Object[parameterTypes.length];
for (int i = 0; i < parameterTypes.length; i++) {
Class<?> paramType = parameterTypes[i];
- paramArgs[i] = Matchers.any(paramType);
+ paramArgs[i] = createParamArgMatcher(paramType);
}
final OngoingStubbing<T> ongoingStubbing = createNewSubstituteMock(mockType, parameterTypes, paramArgs);
Constructor<?>[] otherCtors = new Constructor<?>[allConstructors.length - 1];
System.arraycopy(allConstructors, 1, otherCtors, 0, allConstructors.length - 1);
return new DelegatingToConstructorsOngoingStubbing<T>(otherCtors, ongoingStubbing);
}
+ abstract Object createParamArgMatcher(Class<?> paramType);
+
@Override
public OngoingStubbing<T> withNoArguments() throws Exception {
return createNewSubstituteMock(mockType, parameterTypes);
@@ -33,14 +33,12 @@
public class MockitoNewInvocationControl<T> implements NewInvocationControl<OngoingStubbing<T>> {
private final InvocationSubstitute<T> substitute;
- private final MockingProgress mockingProgress;
public MockitoNewInvocationControl(InvocationSubstitute<T> substitute) {
if (substitute == null) {
throw new IllegalArgumentException("Internal error: substitute cannot be null.");
}
this.substitute = substitute;
- this.mockingProgress = new ThreadSafeMockingProgress();
}
@Override
@@ -15,15 +15,27 @@
*/
package org.powermock.api.mockito.internal.expectation;
+import org.mockito.Matchers;
+import org.mockito.stubbing.OngoingStubbing;
+import org.powermock.api.mockito.internal.invocation.MockitoNewInvocationControl;
import org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator;
import org.powermock.api.mockito.internal.mockcreation.MockCreator;
+import org.powermock.core.spi.NewInvocationControl;
+import org.powermock.core.spi.support.InvocationSubstitute;
public class DefaultConstructorExpectationSetup<T> extends AbstractConstructorExpectationSetup<T> {
public DefaultConstructorExpectationSetup(Class<T> mockType) {
super(mockType);
}
- MockCreator getMockCreator() {return new DefaultMockCreator();}
+ MockCreator getMockCreator() {return new DefaultMockCreator();}
+ <T> NewInvocationControl<OngoingStubbing<T>> createNewInvocationControl(InvocationSubstitute<T> mock) {
+ return new MockitoNewInvocationControl(mock);
+ }
+
+ Object createParamArgMatcher(Class<?> paramType) {
+ return Matchers.any(paramType);
+ }
}
@@ -20,7 +20,7 @@
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.MockSettings;
-import org.mockito.MockitoAnnotations.Mock;
+import org.mockito.Mock;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.configuration.InjectingAnnotationEngine;
import org.mockito.internal.util.reflection.GenericMaster;
@@ -0,0 +1,88 @@
+package org.powermock.api.extension.reporter;
+
+import org.powermock.core.reporter.MockingFrameworkReporter;
+import org.powermock.reflect.Whitebox;
+
+import static org.powermock.utils.StringJoiner.join;
+
+/**
+ * The MockingFrameworkReporterFactory which create a new instance of MockingFrameworkReporter
+ * which is loaded by current context class loader.
+ */
+@SuppressWarnings("WeakerAccess")
+public class MockingFrameworkReporterFactoryImpl extends AbstractMockingFrameworkReporterFactory {
+
+ @Override
+ protected String getImplementerClassName() {
+ return "org.powermock.api.extension.reporter.MockingFrameworkReporterFactoryImpl$MockitoMockingFrameworkReporter";
+ }
+
+ @SuppressWarnings("unused")
+ private static class MockitoMockingFrameworkReporter implements MockingFrameworkReporter {
+// TODO: mockito2 reporter is internal now
+// private org.mockito.internal.exceptions.Reporter mockitoReporter;
+// private org.mockito.internal.MockitoCore mockitoCore;
+
+// private org.mockito.internal.exceptions.Reporter getMockitoReporter(Object mockitoCore) {
+
+// return Whitebox.getInternalState(mockitoCore, "reporter");
+// }
+
+// private void setMockitoReporter(org.mockito.internal.exceptions.Reporter reporter, org.mockito.internal.MockitoCore mockitoCore) {
+// Whitebox.setInternalState(mockitoCore, "reporter", reporter);
+// }
+
+ @Override
+ public void enable() {
+// mockitoCore = getMockitoCoreForCurrentClassLoader();
+// mockitoReporter = getMockitoReporter(mockitoCore);
+//
+// PowerMockitoReporter powerMockitoReporter = new PowerMockitoReporter();
+// setMockitoReporter(powerMockitoReporter, mockitoCore);
+ }
+
+// private org.mockito.internal.MockitoCore getMockitoCoreForCurrentClassLoader() {
+// ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+// try {
+// return Whitebox.getInternalState(classLoader.loadClass("org.mockito.Mockito"), "MOCKITO_CORE");
+// } catch (ClassNotFoundException e) {
+// throw new RuntimeException(e);
+// }
+// }
+
+ @Override
+ public void disable() {
+// setMockitoReporter(mockitoReporter, mockitoCore);
+ }
+ }
+
+ /**
+ * PowerMock reported for Mockito, which replace standard mockito message
+ * to specific message for cases when PowerMock is used.
+ */
+// private static class PowerMockitoReporter extends org.mockito.internal.exceptions.Reporter {
+//
+// public void missingMethodInvocation() {
+// throw new org.mockito.exceptions.misusing.MissingMethodInvocationException(join(
+// "when() requires an argument which has to be 'a method call on a mock'.",
+// "For example:",
+// " when(mock.getArticles()).thenReturn(articles);",
+// "Or 'a static method call on a prepared class`",
+// "For example:",
+// " @PrepareForTest( { StaticService.class }) ",
+// " TestClass{",
+// " public void testMethod(){",
+// " PowerMockito.mockStatic(StaticService.class);",
+// " when(StaticService.say()).thenReturn(expected);",
+// " }",
+// " }",
+// "",
+// "Also, this error might show up because:",
+// "1. inside when() you don't call method on mock but on some other object.",
+// "2. inside when() you don't call static method, but class has not been prepared.",
+// ""
+// ));
+// }
+//
+// }
+}
@@ -19,10 +19,12 @@
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;
import org.mockito.internal.stubbing.answers.Returns;
import org.mockito.internal.stubbing.answers.ThrowsException;
+import org.mockito.listeners.VerificationListener;
import org.mockito.stubbing.Answer;
import org.mockito.stubbing.OngoingStubbing;
import org.mockito.verification.VerificationMode;
@@ -47,6 +49,7 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
+import java.util.Set;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.withSettings;
@@ -69,6 +72,7 @@
* the class to enable static mocking
*/
public static synchronized void mockStatic(Class<?> type, Class<?>... types) {
+ ThreadSafeMockingProgress.mockingProgress().reset();
DefaultMockCreator.mock(type, true, false, null, null, (Method[]) null);
if(types != null && types.length > 0) {
for (Class<?> aClass : types) {
@@ -126,6 +130,7 @@ public static void mockStatic(Class<?> classMock, @SuppressWarnings("rawtypes")
* additional mock settings
*/
public static void mockStatic(Class<?> classToMock, MockSettings mockSettings) {
+ ThreadSafeMockingProgress.mockingProgress().reset();
DefaultMockCreator.mock(classToMock, true, false, null, mockSettings, (Method[]) null);
}
@@ -230,13 +235,14 @@ public static void mockStatic(Class<?> classToMock, MockSettings mockSettings) {
* the type of the class mock
*/
public static synchronized <T> void spy(Class<T> type) {
+ ThreadSafeMockingProgress.mockingProgress().reset();
DefaultMockCreator.mock(type, true, true, type, null, (Method[]) null);
}
/**
* Verifies certain behavior <b>happened once</b>
* <p>
- * Alias to {@code verifyStatic(times(1))} E.g:
+ * Alias to <code>verifyStatic(times(1))</code> E.g:
*
* <pre>
* verifyStatic();
@@ -284,7 +290,7 @@ public static synchronized void verifyStatic() {
* times(x), atLeastOnce() or never()
*/
public static synchronized void verifyStatic(VerificationMode verificationMode) {
- Whitebox.getInternalState(Mockito.class, MockingProgress.class).verificationStarted(
+ ThreadSafeMockingProgress.mockingProgress().verificationStarted(
POWERMOCKITO_CORE.wrapInStaticVerificationMode(verificationMode));
}
@@ -308,7 +314,7 @@ public static PrivateMethodVerification verifyPrivate(Object object) throws Exce
*/
public static PrivateMethodVerification verifyPrivate(Object object, VerificationMode verificationMode)
throws Exception {
- Whitebox.getInternalState(Mockito.class, MockingProgress.class).verificationStarted(
+ ThreadSafeMockingProgress.mockingProgress().verificationStarted(
POWERMOCKITO_CORE.wrapInMockitoSpecificVerificationMode(object, verificationMode));
return new DefaultPrivateMethodVerification(object);
}
@@ -340,7 +346,7 @@ public static PrivateMethodVerification verifyPrivate(Class<?> clazz, Verificati
/**
* Verifies certain behavior <b>happened once</b>
* <p>
- * Alias to {@code verifyNew(mockClass, times(1))} E.g:
+ * Alias to <code>verifyNew(mockClass, times(1))</code> E.g:
*
* <pre>
* verifyNew(ClassWithStaticMethod.class);
@@ -18,6 +18,7 @@
import org.mockito.Mockito;
import org.mockito.internal.progress.MockingProgress;
+import org.mockito.internal.progress.ThreadSafeMockingProgress;
import org.mockito.internal.verification.MockAwareVerificationMode;
import org.mockito.stubbing.Answer;
import org.mockito.verification.VerificationMode;
@@ -26,6 +27,8 @@
import org.powermock.api.mockito.internal.verification.StaticMockAwareVerificationMode;
import org.powermock.reflect.Whitebox;
+import java.util.Set;
+
public class PowerMockitoCore {
@SuppressWarnings("rawtypes")
public PowerMockitoStubber doAnswer(Answer answer) {
@@ -43,14 +46,14 @@ public PowerMockitoStubber doAnswer(Answer answer) {
}
private MockingProgress getMockingProgress() {
- return Whitebox.getInternalState(Mockito.class, MockingProgress.class);
+ return ThreadSafeMockingProgress.mockingProgress();
}
public MockAwareVerificationMode wrapInMockitoSpecificVerificationMode(Object mock, VerificationMode mode) {
- return new MockAwareVerificationMode(mock, mode);
+ return new MockAwareVerificationMode(mock, mode, getMockingProgress().verificationListeners());
}
public MockAwareVerificationMode wrapInStaticVerificationMode(VerificationMode mode) {
- return new StaticMockAwareVerificationMode(mode);
+ return new StaticMockAwareVerificationMode(mode, getMockingProgress().verificationListeners());
}
}
@@ -40,7 +40,7 @@ public void process(Class<?> context, Object testClass) {
if (field.isAnnotationPresent(Spy.class)) {
try {
Whitebox.invokeMethod(this, Spy.class, field, new Class<?>[] { Mock.class,
- org.mockito.MockitoAnnotations.Mock.class, Captor.class });
+ org.mockito.Mock.class, Captor.class });
} catch (RuntimeException e) {
throw e;
} catch (Exception e1) {
@@ -24,8 +24,8 @@
public StackTraceCleaner getStackTraceCleaner(final StackTraceCleaner defaultCleaner) {
return new StackTraceCleaner() {
@Override
- public boolean isOut(StackTraceElement candidate) {
- return defaultCleaner.isOut(candidate) || candidate.getClassName().startsWith("org.powermock.api.mockito");
+ public boolean isIn(StackTraceElement candidate) {
+ return defaultCleaner.isIn(candidate) && !candidate.getClassName().startsWith("org.powermock.api.mockito");
}
};
}
@@ -16,7 +16,13 @@
*/
package org.powermock.api.mockito.internal.expectation;
+import org.mockito.stubbing.OngoingStubbing;
+import org.powermock.api.mockito.internal.invocation.MockitoNewInvocationControl;
import org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator;
+import org.powermock.core.spi.NewInvocationControl;
+import org.powermock.core.spi.support.InvocationSubstitute;
+
+import org.mockito.ArgumentMatchers;
public class DefaultConstructorExpectationSetup<T> extends AbstractConstructorExpectationSetup<T> {
@@ -26,4 +32,11 @@ public DefaultConstructorExpectationSetup(Class<T> mockType) {
DefaultMockCreator getMockCreator() {return new DefaultMockCreator();}
+ <T> NewInvocationControl<OngoingStubbing<T>> createNewInvocationControl(InvocationSubstitute<T> mock) {
+ return new MockitoNewInvocationControl(mock);
+ }
+
+ Object createParamArgMatcher(Class<?> paramType) {
+ return ArgumentMatchers.nullable(paramType);
+ }
}
Oops, something went wrong.

0 comments on commit a89e8e5

Please sign in to comment.