From 099377a56f26fa102ba2f68b082c35fef61c5dc4 Mon Sep 17 00:00:00 2001 From: emmaLP Date: Wed, 1 Mar 2017 18:56:03 +0000 Subject: [PATCH] #753 issue fix to upgrade code to be compatible with later versions of Mockito - Updated the Mockito version to a more later version - Fixed issues with imports -Updated version to 1.7.0RC4 --- .gitignore | 5 +- build.gradle | 2 +- .../powermock/api/mockito/PowerMockito.java | 197 +++++++----------- .../AcrossJVMSerializationFeature.java | 2 +- .../mockito/repackaged/ClassImposterizer.java | 2 +- version.properties | 2 +- 6 files changed, 86 insertions(+), 124 deletions(-) diff --git a/.gitignore b/.gitignore index 4f3678b53..3b4c30b35 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,7 @@ target .settings .springBeans repo -.idea \ No newline at end of file +.idea +**/build/** +.gradle +**/classes/** \ No newline at end of file diff --git a/build.gradle b/build.gradle index e3f0f10da..0b89198bd 100644 --- a/build.gradle +++ b/build.gradle @@ -31,7 +31,7 @@ ext{ testngVersion = "6.9.10" xstreamVersion = "1.4.9" mockito1Version = "1.10.19" - mockito2Version = "2.4.0" + mockito2Version = "2.7.5" servletVersion = "2.5" systemRulesVersion = "1.16.0" jacocoVersion = "0.7.7.201606060606" diff --git a/powermock-api/powermock-api-mockito2/src/main/java/org/powermock/api/mockito/PowerMockito.java b/powermock-api/powermock-api-mockito2/src/main/java/org/powermock/api/mockito/PowerMockito.java index 4efb19e95..c35c8257d 100644 --- a/powermock-api/powermock-api-mockito2/src/main/java/org/powermock/api/mockito/PowerMockito.java +++ b/powermock-api/powermock-api-mockito2/src/main/java/org/powermock/api/mockito/PowerMockito.java @@ -18,13 +18,11 @@ 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; @@ -49,7 +47,6 @@ 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; @@ -68,13 +65,12 @@ public class PowerMockito extends MemberModifier { /** * Enable static mocking for all methods of a class. * - * @param type - * the class to enable static mocking + * @param type 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) { + if (types != null && types.length > 0) { for (Class aClass : types) { DefaultMockCreator.mock(aClass, true, false, null, null, (Method[]) null); } @@ -89,16 +85,14 @@ public static synchronized void mockStatic(Class type, Class... types) { *

* It is the default answer so it will be used only when you don't * stub the method call. - * + *

*

      * mockStatic(Foo.class, RETURNS_SMART_NULLS);
      * mockStatic(Foo.class, new YourOwnAnswer());
      * 
* - * @param classMock - * class to mock - * @param defaultAnswer - * default answer for unstubbed methods + * @param classMock class to mock + * @param defaultAnswer default answer for unstubbed methods */ public static void mockStatic(Class classMock, @SuppressWarnings("rawtypes") Answer defaultAnswer) { mockStatic(classMock, withSettings().defaultAnswer(defaultAnswer)); @@ -110,13 +104,13 @@ public static void mockStatic(Class classMock, @SuppressWarnings("rawtypes") * The number of configuration points for a mock grows so we need a fluent * way to introduce new configuration without adding more and more * overloaded PowerMockito.mockStatic() methods. Hence {@link MockSettings}. - * + *

*

      *   mockStatic(Listener.class, withSettings()
      *     .name("firstListner").defaultBehavior(RETURNS_SMART_NULLS));
      *   );
      * 
- * + *

* Use it carefully and occasionally. What might be reason your test * needs non-standard mocks? Is the code under test so complicated that it * requires non-standard mocks? Wouldn't you prefer to refactor the code @@ -124,10 +118,8 @@ public static void mockStatic(Class classMock, @SuppressWarnings("rawtypes") *

* See also {@link Mockito#withSettings()} * - * @param classToMock - * class to mock - * @param mockSettings - * additional mock settings + * @param classToMock class to mock + * @param mockSettings additional mock settings */ public static void mockStatic(Class classToMock, MockSettings mockSettings) { ThreadSafeMockingProgress.mockingProgress().reset(); @@ -137,10 +129,8 @@ public static void mockStatic(Class classToMock, MockSettings mockSettings) { /** * Creates a mock object that supports mocking of final and native methods. * - * @param - * the type of the mock object - * @param type - * the type of the mock object + * @param the type of the mock object + * @param type the type of the mock object * @return the mock object. */ public static synchronized T mock(Class type) { @@ -154,21 +144,18 @@ public static synchronized T mock(Class type) { *

* It is the default answer so it will be used only when you don't * stub the method call. - * + *

*

      * Foo mock = mock(Foo.class, RETURNS_SMART_NULLS);
      * Foo mockTwo = mock(Foo.class, new YourOwnAnswer());
      * 
- * + *

*

* See examples in javadoc for {@link Mockito} class *

* - * @param classToMock - * class or interface to mock - * @param defaultAnswer - * default answer for unstubbed methods - * + * @param classToMock class or interface to mock + * @param defaultAnswer default answer for unstubbed methods * @return mock object */ public static T mock(Class classToMock, @SuppressWarnings("rawtypes") Answer defaultAnswer) { @@ -181,13 +168,13 @@ public static T mock(Class classToMock, @SuppressWarnings("rawtypes") Ans * The number of configuration points for a mock grows so we need a fluent * way to introduce new configuration without adding more and more * overloaded Mockito.mock() methods. Hence {@link MockSettings}. - * + *

*

      *   Listener mock = mock(Listener.class, withSettings()
      *     .name("firstListner").defaultBehavior(RETURNS_SMART_NULLS));
      *   );
      * 
- * + *

* Use it carefully and occasionally. What might be reason your test * needs non-standard mocks? Is the code under test so complicated that it * requires non-standard mocks? Wouldn't you prefer to refactor the code @@ -197,10 +184,8 @@ public static T mock(Class classToMock, @SuppressWarnings("rawtypes") Ans *

* See examples in javadoc for {@link Mockito} class * - * @param classToMock - * class or interface to mock - * @param mockSettings - * additional mock settings + * @param classToMock class or interface to mock + * @param mockSettings additional mock settings * @return mock object */ public static T mock(Class classToMock, MockSettings mockSettings) { @@ -211,13 +196,10 @@ public static T mock(Class classToMock, MockSettings mockSettings) { * Spy on objects that are final or otherwise not "spyable" from * normal Mockito. * - * @see Mockito#spy(Object) - * - * @param - * the type of the mock object - * @param object - * the object to spy on + * @param the type of the mock object + * @param object the object to spy on * @return the spy object. + * @see Mockito#spy(Object) */ @SuppressWarnings("unchecked") public static synchronized T spy(T object) { @@ -227,12 +209,9 @@ public static synchronized T spy(T object) { /** * Spy on classes (not "spyable" from normal Mockito). * + * @param the type of the class mock + * @param type the type of the class mock * @see Mockito#spy(Object) - * - * @param - * the type of the class mock - * @param type - * the type of the class mock */ public static synchronized void spy(Class type) { ThreadSafeMockingProgress.mockingProgress().reset(); @@ -243,19 +222,19 @@ public static synchronized void spy(Class type) { * Verifies certain behavior happened once *

* Alias to verifyStatic(times(1)) E.g: - * + *

*

      * verifyStatic();
      * ClassWithStaticMethod.someStaticMethod("some arg");
      * 
- * + *

* Above is equivalent to: - * + *

*

      * verifyStatic(times(1));
      * ClassWithStaticMethod.someStaticMethod("some arg");
      * 
- * + *

*

* Although it is possible to verify a stubbed invocation, usually it's * just redundant. Let's say you've stubbed foo.bar(). If your code @@ -270,7 +249,7 @@ public static synchronized void verifyStatic() { /** * Verifies certain behavior happened at least once / exact number of times * / never. E.g: - * + *

*

      *   verifyStatic(times(5));
      *   ClassWithStaticMethod.someStaticMethod("was called five times");
@@ -282,12 +261,11 @@ public static synchronized void verifyStatic() {
      *   verifyStatic(atLeastOnce());
      *   ClassWithStaticMethod.someMethod(<b>anyString()</b>);
      * 
- * + *

* times(1) is the default and can be omitted *

* - * @param verificationMode - * times(x), atLeastOnce() or never() + * @param verificationMode times(x), atLeastOnce() or never() */ public static synchronized void verifyStatic(VerificationMode verificationMode) { ThreadSafeMockingProgress.mockingProgress().verificationStarted( @@ -297,9 +275,8 @@ public static synchronized void verifyStatic(VerificationMode verificationMode) /** * Verify a private method invocation for an instance. * + * @throws Exception If something unexpected goes wrong. * @see {@link Mockito#verify(Object)} - * @throws Exception - * If something unexpected goes wrong. */ public static PrivateMethodVerification verifyPrivate(Object object) throws Exception { return verifyPrivate(object, times(1)); @@ -308,9 +285,8 @@ public static PrivateMethodVerification verifyPrivate(Object object) throws Exce /** * Verify a private method invocation with a given verification mode. * + * @throws Exception If something unexpected goes wrong. * @see {@link Mockito#verify(Object)} - * @throws Exception - * If something unexpected goes wrong. */ public static PrivateMethodVerification verifyPrivate(Object object, VerificationMode verificationMode) throws Exception { @@ -322,9 +298,8 @@ public static PrivateMethodVerification verifyPrivate(Object object, Verificatio /** * Verify a private method invocation for a class. * + * @throws Exception If something unexpected goes wrong. * @see {@link Mockito#verify(Object)} - * @throws Exception - * If something unexpected goes wrong. */ public static PrivateMethodVerification verifyPrivate(Class clazz) throws Exception { return verifyPrivate((Object) clazz); @@ -334,9 +309,8 @@ public static PrivateMethodVerification verifyPrivate(Class clazz) throws Exc * Verify a private method invocation for a class with a given verification * mode. * + * @throws Exception If something unexpected goes wrong. * @see {@link Mockito#verify(Object)} - * @throws Exception - * If something unexpected goes wrong. */ public static PrivateMethodVerification verifyPrivate(Class clazz, VerificationMode verificationMode) throws Exception { @@ -347,21 +321,20 @@ public static PrivateMethodVerification verifyPrivate(Class clazz, Verificati * Verifies certain behavior happened once *

* Alias to verifyNew(mockClass, times(1)) E.g: - * + *

*

      * verifyNew(ClassWithStaticMethod.class);
      * 
- * + *

* Above is equivalent to: - * + *

*

      * verifyNew(ClassWithStaticMethod.class, times(1));
      * 
- * + *

*

* - * @param mock - * Class mocked by PowerMock. + * @param mock Class mocked by PowerMock. */ @SuppressWarnings("unchecked") public static synchronized ConstructorArgumentsVerification verifyNew(Class mock) { @@ -380,7 +353,7 @@ public static synchronized ConstructorArgumentsVerification verifyNew(Class< /** * Verifies certain behavior happened at least once / exact number of times * / never. E.g: - * + *

*

      * verifyNew(ClassWithStaticMethod.class, times(5));
      *
@@ -389,14 +362,12 @@ public static synchronized  ConstructorArgumentsVerification verifyNew(Class<
      * //you can use flexible argument matchers, e.g:
      * verifyNew(ClassWithStaticMethod.class, atLeastOnce());
      * 
- * + *

* times(1) is the default and can be omitted *

* - * @param mock - * to be verified - * @param mode - * times(x), atLeastOnce() or never() + * @param mock to be verified + * @param mode times(x), atLeastOnce() or never() */ @SuppressWarnings("unchecked") public static ConstructorArgumentsVerification verifyNew(Class mock, VerificationMode mode) { @@ -423,20 +394,18 @@ public static ConstructorArgumentsVerification verifyNew(Class mock, Veri /** * Expect calls to private methods. * + * @throws Exception If something unexpected goes wrong. * @see {@link Mockito#when(Object)} - * @throws Exception - * If something unexpected goes wrong. */ public static OngoingStubbing when(Object instance, String methodName, Object... arguments) throws Exception { - return Mockito.when(Whitebox. invokeMethod(instance, methodName, arguments)); + return Mockito.when(Whitebox.invokeMethod(instance, methodName, arguments)); } /** * Expect calls to private methods. * + * @throws Exception If something unexpected goes wrong. * @see {@link Mockito#when(Object)} - * @throws Exception - * If something unexpected goes wrong. */ public static WithOrWithoutExpectedArguments when(Object instance, Method method) throws Exception { return new DefaultMethodExpectationSetup(instance, method); @@ -445,9 +414,8 @@ public static WithOrWithoutExpectedArguments when(Object instance, Method /** * Expect calls to private static methods. * + * @throws Exception If something unexpected goes wrong. * @see {@link Mockito#when(Object)} - * @throws Exception - * If something unexpected goes wrong. */ public static WithOrWithoutExpectedArguments when(Class cls, Method method) throws Exception { return new DefaultMethodExpectationSetup(cls, method); @@ -458,24 +426,22 @@ public static WithOrWithoutExpectedArguments when(Class cls, Method me * name. The method will be looked up using the parameter types (if * possible). * + * @throws Exception If something unexpected goes wrong. * @see {@link Mockito#when(Object)} - * @throws Exception - * If something unexpected goes wrong. */ public static OngoingStubbing when(Object instance, Object... arguments) throws Exception { - return Mockito.when(Whitebox. invokeMethod(instance, arguments)); + return Mockito.when(Whitebox.invokeMethod(instance, arguments)); } /** * Expect a static private or inner class method call. * + * @throws Exception If something unexpected goes wrong. * @see {@link Mockito#when(Object)} - * @throws Exception - * If something unexpected goes wrong. */ public static OngoingStubbing when(Class clazz, String methodToExpect, Object... arguments) throws Exception { - return Mockito.when(Whitebox. invokeMethod(clazz, methodToExpect, arguments)); + return Mockito.when(Whitebox.invokeMethod(clazz, methodToExpect, arguments)); } /** @@ -483,12 +449,11 @@ public static OngoingStubbing when(Class clazz, String methodToExpect, * method name. The method will be looked up using the parameter types if * possible * + * @throws Exception If something unexpected goes wrong. * @see {@link Mockito#when(Object)} - * @throws Exception - * If something unexpected goes wrong. */ public static OngoingStubbing when(Class klass, Object... arguments) throws Exception { - return Mockito.when(Whitebox. invokeMethod(klass, arguments)); + return Mockito.when(Whitebox.invokeMethod(klass, arguments)); } /** @@ -521,9 +486,8 @@ public static synchronized ConstructorExpectationSetup whenNew(Class t * (inner) classes, local or anonymous classes. For example you might want * to throw an exception or return a mock. * - * @param fullyQualifiedName - * The fully-qualified name of the inner/local/anonymous type to - * expect. + * @param fullyQualifiedName The fully-qualified name of the inner/local/anonymous type to + * expect. */ @SuppressWarnings("unchecked") public static synchronized ConstructorExpectationSetup whenNew(String fullyQualifiedName) throws Exception { @@ -559,10 +523,10 @@ public static synchronized ConstructorExpectationSetup whenNew(String ful * the test method, for example: in setUp(), @Before method or in * constructor. Consider writing nice code that makes interactions only in * test methods. - * + *

*

* Example: - * + *

*

      * //interactions
      * mock.doSomething();
@@ -575,11 +539,10 @@ public static synchronized  ConstructorExpectationSetup whenNew(String ful
      * verifyNoMoreInteractions(mock);
      *
      * 
- * + *

* See examples in javadoc for {@link Mockito} class * - * @param mocks - * to be verified + * @param mocks to be verified */ public static void verifyNoMoreInteractions(Object... mocks) { VerifyNoMoreInteractions.verifyNoMoreInteractions(mocks); @@ -590,11 +553,11 @@ public static void verifyNoMoreInteractions(Object... mocks) { * instance and class mocks). Delegates to the orignal * {@link Mockito#verifyNoMoreInteractions(Object...)} if the mock is not a * PowerMockito mock. - * + *

*

      * verifyZeroInteractions(mockOne, mockTwo);
      * 
- * + *

* This method will also detect invocations that occurred before the test * method, for example: in setUp(), @Before method or in constructor. * Consider writing nice code that makes interactions only in test methods. @@ -604,8 +567,7 @@ public static void verifyNoMoreInteractions(Object... mocks) { *

* See examples in javadoc for {@link Mockito} class * - * @param mocks - * to be verified + * @param mocks to be verified */ public static void verifyZeroInteractions(Object... mocks) { VerifyNoMoreInteractions.verifyNoMoreInteractions(mocks); @@ -620,7 +582,7 @@ public static void verifyZeroInteractions(Object... mocks) { * methods inside brackets... *

* Example: - * + *

*

      * doAnswer(new Answer() {
      *     public Object answer(InvocationOnMock invocation) {
@@ -633,8 +595,7 @@ public static void verifyZeroInteractions(Object... mocks) {
      * 

* See examples in javadoc for {@link Mockito} class * - * @param answer - * to answer when the stubbed method is called + * @param answer to answer when the stubbed method is called * @return stubber - to select a method for stubbing */ public static PowerMockitoStubber doAnswer(Answer answer) { @@ -649,13 +610,12 @@ public static PowerMockitoStubber doAnswer(Answer answer) { * methods inside brackets... *

* Example: - * + *

*

      * doThrow(new RuntimeException()).when(mock).someVoidMethod();
      * 
* - * @param toBeThrown - * to be thrown when the stubbed method is called + * @param toBeThrown to be thrown when the stubbed method is called * @return stubber - to select a method for stubbing */ public static PowerMockitoStubber doThrow(Throwable toBeThrown) { @@ -686,7 +646,7 @@ public static PowerMockitoStubber doThrow(Throwable toBeThrown) { * the object passed to spy() method. *

* Example: - * + *

*

      * Foo mock = mock(Foo.class);
      * doCallRealMethod().when(mock).someVoidMethod();
@@ -709,7 +669,7 @@ public static PowerMockitoStubber doCallRealMethod() {
      * situations when doNothing() comes handy:
      * 

* 1. Stubbing consecutive calls on a void method: - * + *

*

      * doNothing().doThrow(new RuntimeException()).when(mock).someVoidMethod();
      *
@@ -719,9 +679,9 @@ public static PowerMockitoStubber doCallRealMethod() {
      * //throws RuntimeException the next time:
      * mock.someVoidMethod();
      * 
- * + *

* 2. When you spy real objects and you want the void method to do nothing: - * + *

*

      * List list = new LinkedList();
      * List spy = spy(list);
@@ -740,7 +700,7 @@ public static PowerMockitoStubber doCallRealMethod() {
      * @return stubber - to select a method for stubbing
      */
     public static PowerMockitoStubber doNothing() {
-        return POWERMOCKITO_CORE.doAnswer(new DoesNothing());
+        return POWERMOCKITO_CORE.doAnswer(DoesNothing.doesNothing());
     }
 
     /**
@@ -753,10 +713,10 @@ public static PowerMockitoStubber doNothing() {
      * 

* Here are those rare occasions when doReturn() comes handy: *

- * + *

* 1. When spying real objects and calling real methods on a spy brings side * effects - * + *

*

      * List list = new LinkedList();
      * List spy = spy(list);
@@ -767,9 +727,9 @@ public static PowerMockitoStubber doNothing() {
      * //You have to use doReturn() for stubbing:
      * doReturn("foo").when(spy).get(0);
      * 
- * + *

* 2. Overriding a previous exception-stubbing: - * + *

*

      * when(mock.foo()).thenThrow(new RuntimeException());
      *
@@ -779,15 +739,14 @@ public static PowerMockitoStubber doNothing() {
      * //You have to use doReturn() for stubbing:
      * doReturn("bar").when(mock).foo();
      * 
- * + *

* Above scenarios shows a tradeoff of Mockito's ellegant syntax. Note that * the scenarios are very rare, though. Spying should be sporadic and * overriding exception-stubbing is very rare. *

* See examples in javadoc for {@link Mockito} class * - * @param toBeReturned - * to be returned when the stubbed method is called + * @param toBeReturned to be returned when the stubbed method is called * @return stubber - to select a method for stubbing */ public static PowerMockitoStubber doReturn(Object toBeReturned) { diff --git a/powermock-api/powermock-api-mockito2/src/main/java/org/powermock/api/mockito/repackaged/AcrossJVMSerializationFeature.java b/powermock-api/powermock-api-mockito2/src/main/java/org/powermock/api/mockito/repackaged/AcrossJVMSerializationFeature.java index fa492a5c6..1eb2d2fbc 100644 --- a/powermock-api/powermock-api-mockito2/src/main/java/org/powermock/api/mockito/repackaged/AcrossJVMSerializationFeature.java +++ b/powermock-api/powermock-api-mockito2/src/main/java/org/powermock/api/mockito/repackaged/AcrossJVMSerializationFeature.java @@ -31,7 +31,7 @@ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; -import static org.mockito.internal.util.StringJoiner.join; +import static org.powermock.utils.StringJoiner.join; /** * This is responsible for serializing a mock, it is enabled if the mock is implementing diff --git a/powermock-api/powermock-api-mockito2/src/main/java/org/powermock/api/mockito/repackaged/ClassImposterizer.java b/powermock-api/powermock-api-mockito2/src/main/java/org/powermock/api/mockito/repackaged/ClassImposterizer.java index bff6f3f71..bb0aa29b5 100644 --- a/powermock-api/powermock-api-mockito2/src/main/java/org/powermock/api/mockito/repackaged/ClassImposterizer.java +++ b/powermock-api/powermock-api-mockito2/src/main/java/org/powermock/api/mockito/repackaged/ClassImposterizer.java @@ -23,7 +23,7 @@ import java.util.Collection; import java.util.List; -import static org.mockito.internal.util.StringJoiner.join; +import static org.powermock.utils.StringJoiner.join; /** * Inspired on jMock (thanks jMock guys!!!) diff --git a/version.properties b/version.properties index 0f129eca6..6e975eb1a 100644 --- a/version.properties +++ b/version.properties @@ -1 +1 @@ -version=1.7.0RC3 \ No newline at end of file +version=1.7.0RC4 \ No newline at end of file