Class PowerMock


  • public class PowerMock
    extends MemberModifier
    PowerMock extends EasyMock functionality with several new features such as mocking static and private methods, mocking new instances and more. Use PowerMock instead of EasyMock where applicable.
    • Constructor Detail

      • PowerMock

        public PowerMock()
    • Method Detail

      • createMock

        public static <T> T createMock​(Class<T> type,
                                       Method... methods)
        Creates a mock object that supports mocking of final and native methods.
        Type Parameters:
        T - the type of the mock object
        Parameters:
        type - the type of the mock object
        methods - optionally what methods to mock
        Returns:
        the mock object.
      • createMock

        public static <T> T createMock​(Class<T> type)
        Creates a mock object that supports mocking of final and native methods.
        Type Parameters:
        T - the type of the mock object
        Parameters:
        type - the type of the mock object
        Returns:
        the mock object.
      • createMock

        public static <T> T createMock​(Class<T> type,
                                       org.easymock.ConstructorArgs constructorArgs,
                                       Method... methods)
        Creates a mock object that supports mocking of final and native methods and invokes a specific constructor.
        Type Parameters:
        T - the type of the mock object
        Parameters:
        type - the type of the mock object
        constructorArgs - The constructor arguments that will be used to invoke a special constructor.
        methods - optionally what methods to mock
        Returns:
        the mock object.
      • createMock

        public static <T> T createMock​(Class<T> type,
                                       Object... constructorArguments)
        Creates a mock object that supports mocking of final and native methods and invokes a specific constructor based on the supplied argument values.
        Type Parameters:
        T - the type of the mock object
        Parameters:
        type - the type of the mock object
        constructorArguments - The constructor arguments that will be used to invoke a certain constructor.
        Returns:
        the mock object.
      • createStrictMock

        public static <T> T createStrictMock​(Class<T> type,
                                             Method... methods)
        Creates a strict mock object that supports mocking of final and native methods.
        Type Parameters:
        T - the type of the mock object
        Parameters:
        type - the type of the mock object
        methods - optionally what methods to mock
        Returns:
        the mock object.
      • createStrictMock

        public static <T> T createStrictMock​(Class<T> type)
        Creates a strict mock object that supports mocking of final and native methods.
        Type Parameters:
        T - the type of the mock object
        Parameters:
        type - the type of the mock object
        Returns:
        the mock object.
      • createNiceMock

        public static <T> T createNiceMock​(Class<T> type,
                                           Method... methods)
        Creates a nice mock object that supports mocking of final and native methods.
        Type Parameters:
        T - the type of the mock object
        Parameters:
        type - the type of the mock object
        methods - optionally what methods to mock
        Returns:
        the mock object.
      • createNiceMock

        public static <T> T createNiceMock​(Class<T> type)
        Creates a nice mock object that supports mocking of final and native methods.
        Type Parameters:
        T - the type of the mock object
        Parameters:
        type - the type of the mock object
        Returns:
        the mock object.
      • createStrictMock

        public static <T> T createStrictMock​(Class<T> type,
                                             org.easymock.ConstructorArgs constructorArgs,
                                             Method... methods)
        Creates a strict mock object that supports mocking of final and native methods and invokes a specific constructor.
        Type Parameters:
        T - the type of the mock object
        Parameters:
        type - the type of the mock object
        constructorArgs - The constructor arguments that will be used to invoke a special constructor.
        methods - optionally what methods to mock
        Returns:
        the mock object.
      • createNiceMock

        public static <T> T createNiceMock​(Class<T> type,
                                           org.easymock.ConstructorArgs constructorArgs,
                                           Method... methods)
        Creates a nice mock object that supports mocking of final and native methods and invokes a specific constructor.
        Type Parameters:
        T - the type of the mock object
        Parameters:
        type - the type of the mock object
        constructorArgs - The constructor arguments that will be used to invoke a special constructor.
        methods - optionally what methods to mock
        Returns:
        the mock object.
      • createStrictMock

        public static <T> T createStrictMock​(Class<T> type,
                                             Object... constructorArguments)
        Creates a strict mock object that supports mocking of final and native methods and invokes a specific constructor based on the supplied argument values.
        Type Parameters:
        T - the type of the mock object
        Parameters:
        type - the type of the mock object
        constructorArguments - The constructor arguments that will be used to invoke a certain constructor.
        Returns:
        the mock object.
      • createNiceMock

        public static <T> T createNiceMock​(Class<T> type,
                                           Object... constructorArguments)
        Creates a nice mock object that supports mocking of final and native methods and invokes a specific constructor based on the supplied argument values.
        Type Parameters:
        T - the type of the mock object
        Parameters:
        type - the type of the mock object
        constructorArguments - The constructor arguments that will be used to invoke a certain constructor.
        Returns:
        the mock object.
      • mockStatic

        public static void mockStatic​(Class<?> type,
                                      Method... methods)
        Enable static mocking for a class.
        Parameters:
        type - the class to enable static mocking
        methods - optionally what methods to mock
      • mockStatic

        public static void mockStatic​(Class<?> type)
        Enable static mocking for a class.
        Parameters:
        type - the class to enable static mocking
      • mockStaticStrict

        public static void mockStaticStrict​(Class<?> type,
                                            Method... methods)
        Enable strict static mocking for a class.
        Parameters:
        type - the class to enable static mocking
        methods - optionally what methods to mock
      • mockStaticStrict

        public static void mockStaticStrict​(Class<?> type)
        Enable strict static mocking for a class.
        Parameters:
        type - the class to enable static mocking
      • mockStaticNice

        public static void mockStaticNice​(Class<?> type,
                                          Method... methods)
        Enable nice static mocking for a class.
        Parameters:
        type - the class to enable static mocking
        methods - optionally what methods to mock
      • mockStaticNice

        public static void mockStaticNice​(Class<?> type)
        Enable nice static mocking for a class.
        Parameters:
        type - the class to enable static mocking
      • createPartialMockForAllMethodsExcept

        public static <T> T createPartialMockForAllMethodsExcept​(Class<T> type,
                                                                 String... methodNames)
        A utility method that may be used to specify several methods that should not be mocked in an easy manner (by just passing in the method names of the method you wish not to mock). Note that you cannot uniquely specify a method to exclude using this method if there are several methods with the same name in type. This method will mock ALL methods that doesn't match the supplied name(s) regardless of parameter types and signature. If this is not the case you should fall-back on using the createMock(Class, Method...) method instead.
        Type Parameters:
        T - The type of the mock.
        Parameters:
        type - The type that'll be used to create a mock instance.
        methodNames - The names of the methods that should be mocked. If null, then this method will have the same effect as just calling createMock(Class, Method...) with the second parameter as new Method[0] (i.e. all methods in that class will be mocked).
        Returns:
        A mock object of type .
      • createNicePartialMockForAllMethodsExcept

        public static <T> T createNicePartialMockForAllMethodsExcept​(Class<T> type,
                                                                     String... methodNames)
        A utility method that may be used to specify several methods that should not be nicely mocked in an easy manner (by just passing in the method names of the method you wish not to mock). Note that you cannot uniquely specify a method to exclude using this method if there are several methods with the same name in type. This method will mock ALL methods that doesn't match the supplied name(s) regardless of parameter types and signature. If this is not the case you should fall-back on using the createMock(Class, Method...) method instead.
        Type Parameters:
        T - The type of the mock.
        Parameters:
        type - The type that'll be used to create a mock instance.
        methodNames - The names of the methods that should be mocked. If null, then this method will have the same effect as just calling createMock(Class, Method...) with the second parameter as new Method[0] (i.e. all methods in that class will be mocked).
        Returns:
        A mock object of type .
      • createStrictPartialMockForAllMethodsExcept

        public static <T> T createStrictPartialMockForAllMethodsExcept​(Class<T> type,
                                                                       String... methodNames)
        A utility method that may be used to specify several methods that should not be strictly mocked in an easy manner (by just passing in the method names of the method you wish not to mock). Note that you cannot uniquely specify a method to exclude using this method if there are several methods with the same name in type. This method will mock ALL methods that doesn't match the supplied name(s) regardless of parameter types and signature. If this is not the case you should fall-back on using the createMock(Class, Method...) method instead.
        Type Parameters:
        T - The type of the mock.
        Parameters:
        type - The type that'll be used to create a mock instance.
        methodNames - The names of the methods that should be mocked. If null, then this method will have the same effect as just calling createMock(Class, Method...) with the second parameter as new Method[0] (i.e. all methods in that class will be mocked).
        Returns:
        A mock object of type .
      • createPartialMockForAllMethodsExcept

        public static <T> T createPartialMockForAllMethodsExcept​(Class<T> type,
                                                                 String methodNameToExclude,
                                                                 Class<?> firstArgumentType,
                                                                 Class<?>... moreTypes)
        Mock all methods of a class except for a specific one. Use this method only if you have several overloaded methods.
        Type Parameters:
        T - The type of the mock.
        Parameters:
        type - The type that'll be used to create a mock instance.
        methodNameToExclude - The name of the method not to mock.
        firstArgumentType - The type of the first parameter of the method not to mock
        moreTypes - Optionally more parameter types that defines the method. Note that this is only needed to separate overloaded methods.
        Returns:
        A mock object of type .
      • createNicePartialMockForAllMethodsExcept

        public static <T> T createNicePartialMockForAllMethodsExcept​(Class<T> type,
                                                                     String methodNameToExclude,
                                                                     Class<?> firstArgumentType,
                                                                     Class<?>... moreTypes)
        Mock all methods of a class except for a specific one nicely. Use this method only if you have several overloaded methods.
        Type Parameters:
        T - The type of the mock.
        Parameters:
        type - The type that'll be used to create a mock instance.
        methodNameToExclude - The name of the method not to mock.
        firstArgumentType - The type of the first parameter of the method not to mock
        moreTypes - Optionally more parameter types that defines the method. Note that this is only needed to separate overloaded methods.
        Returns:
        A mock object of type .
      • createStrictPartialMockForAllMethodsExcept

        public static <T> T createStrictPartialMockForAllMethodsExcept​(Class<T> type,
                                                                       String methodNameToExclude,
                                                                       Class<?> firstArgumentType,
                                                                       Class<?>... moreTypes)
        Mock all methods of a class except for a specific one strictly. Use this method only if you have several overloaded methods.
        Type Parameters:
        T - The type of the mock.
        Parameters:
        type - The type that'll be used to create a mock instance.
        methodNameToExclude - The name of the method not to mock.
        firstArgumentType - The type of the first parameter of the method not to mock
        moreTypes - Optionally more parameter types that defines the method. Note that this is only needed to separate overloaded methods.
        Returns:
        A mock object of type .
      • createPartialMock

        public static <T> T createPartialMock​(Class<T> type,
                                              String methodNameToMock,
                                              Class<?> firstArgumentType,
                                              Class<?>... additionalArgumentTypes)
        Mock a single specific method. Use this to handle overloaded methods.
        Type Parameters:
        T - The type of the mock.
        Parameters:
        type - The type that'll be used to create a mock instance.
        methodNameToMock - The name of the method to mock
        firstArgumentType - The type of the first parameter of the method to mock
        additionalArgumentTypes - Optionally more parameter types that defines the method. Note that this is only needed to separate overloaded methods.
        Returns:
        A mock object of type .
      • createStrictPartialMock

        public static <T> T createStrictPartialMock​(Class<T> type,
                                                    String methodNameToMock,
                                                    Class<?> firstArgumentType,
                                                    Class<?>... additionalArgumentTypes)
        Strictly mock a single specific method. Use this to handle overloaded methods.
        Type Parameters:
        T - The type of the mock.
        Parameters:
        type - The type that'll be used to create a mock instance.
        methodNameToMock - The name of the method to mock
        firstArgumentType - The type of the first parameter of the method to mock
        additionalArgumentTypes - Optionally more parameter types that defines the method. Note that this is only needed to separate overloaded methods.
        Returns:
        A mock object of type .
      • createNicePartialMock

        public static <T> T createNicePartialMock​(Class<T> type,
                                                  String methodNameToMock,
                                                  Class<?> firstArgumentType,
                                                  Class<?>... additionalArgumentTypes)
        Nicely mock a single specific method. Use this to handle overloaded methods.
        Type Parameters:
        T - The type of the mock.
        Parameters:
        type - The type that'll be used to create a mock instance.
        methodNameToMock - The name of the method to mock
        firstArgumentType - The type of the first parameter of the method to mock
        additionalArgumentTypes - Optionally more parameter types that defines the method. Note that this is only needed to separate overloaded methods.
        Returns:
        A mock object of type .
      • mockStaticPartial

        public static void mockStaticPartial​(Class<?> clazz,
                                             String methodNameToMock,
                                             Class<?> firstArgumentType,
                                             Class<?>... additionalArgumentTypes)
        Mock a single static method.
        Parameters:
        clazz - The class where the method is specified in.
        methodNameToMock - The first argument
        firstArgumentType - The first argument type.
        additionalArgumentTypes - Optional additional argument types.
      • mockStaticPartialStrict

        public static void mockStaticPartialStrict​(Class<?> clazz,
                                                   String methodNameToMock,
                                                   Class<?> firstArgumentType,
                                                   Class<?>... additionalArgumentTypes)
        Mock a single static method (strict).
        Parameters:
        clazz - The class where the method is specified in.
        methodNameToMock - The first argument
        firstArgumentType - The first argument type.
        additionalArgumentTypes - Optional additional argument types.
      • mockStaticPartialNice

        public static void mockStaticPartialNice​(Class<?> clazz,
                                                 String methodNameToMock,
                                                 Class<?> firstArgumentType,
                                                 Class<?>... additionalArgumentTypes)
        Mock a single static method (nice).
        Parameters:
        clazz - The class where the method is specified in.
        methodNameToMock - The first argument
        firstArgumentType - The first argument type.
        additionalArgumentTypes - Optional additional argument types.
      • mockStaticPartial

        public static void mockStaticPartial​(Class<?> clazz,
                                             String... methodNames)
        A utility method that may be used to mock several static methods in an easy way (by just passing in the method names of the method you wish to mock). Note that you cannot uniquely specify a method to mock using this method if there are several methods with the same name in type. This method will mock ALL methods that match the supplied name regardless of parameter types and signature. If this is the case you should fall-back on using the mockStatic(Class, Method...) method instead.
        Parameters:
        clazz - The class that contains the static methods that should be mocked.
        methodNames - The names of the methods that should be mocked. If null, then this method will have the same effect as just calling mockStatic(Class, Method...) with the second parameter as new Method[0] (i.e. all methods in that class will be mocked).
      • mockStaticPartialStrict

        public static void mockStaticPartialStrict​(Class<?> clazz,
                                                   String... methodNames)
        A utility method that may be used to mock several static methods (strict) in an easy way (by just passing in the method names of the method you wish to mock). Note that you cannot uniquely specify a method to mock using this method if there are several methods with the same name in type. This method will mock ALL methods that match the supplied name regardless of parameter types and signature. If this is the case you should fall-back on using the mockStaticStrict(Class, Method...) method instead.
        Parameters:
        clazz - The class that contains the static methods that should be mocked.
        methodNames - The names of the methods that should be mocked. If null, then this method will have the same effect as just calling mockStatic(Class, Method...) with the second parameter as new Method[0] (i.e. all methods in that class will be mocked).
      • mockStaticPartialNice

        public static void mockStaticPartialNice​(Class<?> clazz,
                                                 String... methodNames)
        A utility method that may be used to mock several static methods (nice) in an easy way (by just passing in the method names of the method you wish to mock). Note that you cannot uniquely specify a method to mock using this method if there are several methods with the same name in type. This method will mock ALL methods that match the supplied name regardless of parameter types and signature. If this is the case you should fall-back on using the mockStaticStrict(Class, Method...) method instead.
        Parameters:
        clazz - The class that contains the static methods that should be mocked.
        methodNames - The names of the methods that should be mocked. If null, then this method will have the same effect as just calling mockStatic(Class, Method...) with the second parameter as new Method[0] (i.e. all methods in that class will be mocked).
      • createPartialMock

        public static <T> T createPartialMock​(Class<T> type,
                                              String... methodNames)
        A utility method that may be used to mock several methods in an easy way (by just passing in the method names of the method you wish to mock). Note that you cannot uniquely specify a method to mock using this method if there are several methods with the same name in type. This method will mock ALL methods that match the supplied name regardless of parameter types and signature. If this is the case you should fall-back on using the createMock(Class, Method...) method instead.
        Type Parameters:
        T - The type of the mock.
        Parameters:
        type - The type that'll be used to create a mock instance.
        methodNames - The names of the methods that should be mocked. If null, then this method will have the same effect as just calling createMock(Class, Method...) with the second parameter as new Method[0] (i.e. all methods in that class will be mocked).
        Returns:
        A mock object of type .
      • createPartialMock

        public static <T> T createPartialMock​(Class<T> type,
                                              Class<? super T> where,
                                              String... methodNames)
        A utility method that may be used to mock several methods in an easy way (by just passing in the method names of the method you wish to mock). Note that you cannot uniquely specify a method to mock using this method if there are several methods with the same name in type. This method will mock ALL methods that match the supplied name regardless of parameter types and signature. If this is the case you should fall-back on using the createMock(Class, Method...) method instead.

        With this method you can specify where the class hierarchy the methods are located. This is useful in, for example, situations where class A extends B and both have a method called "mockMe" (A overrides B's mockMe method) and you like to specify the only the "mockMe" method in B should be mocked. "mockMe" in A should be left intact. In this case you should do:

         A tested = createPartialMock(A.class, B.class, "mockMe");
         
        Type Parameters:
        T - The type of the mock.
        Parameters:
        type - The type that'll be used to create a mock instance.
        where - Where in the class hierarchy the methods resides.
        methodNames - The names of the methods that should be mocked. If null, then this method will have the same effect as just calling createMock(Class, Method...) with the second parameter as new Method[0] (i.e. all methods in that class will be mocked).
        Returns:
        A mock object of type .
      • createStrictPartialMock

        public static <T> T createStrictPartialMock​(Class<T> type,
                                                    String... methodNames)
        A utility method that may be used to strictly mock several methods in an easy way (by just passing in the method names of the method you wish to mock). Note that you cannot uniquely specify a method to mock using this method if there are several methods with the same name in type. This method will mock ALL methods that match the supplied name regardless of parameter types and signature. If this is the case you should fall-back on using the createMock(Class, Method...) method instead.
        Type Parameters:
        T - The type of the mock.
        Parameters:
        type - The type that'll be used to create a mock instance.
        methodNames - The names of the methods that should be mocked. If null, then this method will have the same effect as just calling createMock(Class, Method...) with the second parameter as new Method[0] (i.e. all methods in that class will be mocked).
        Returns:
        A mock object of type .
      • createStrictPartialMock

        public static <T> T createStrictPartialMock​(Class<T> type,
                                                    Class<? super T> where,
                                                    String... methodNames)
        A utility method that may be used to strictly mock several methods in an easy way (by just passing in the method names of the method you wish to mock). Note that you cannot uniquely specify a method to mock using this method if there are several methods with the same name in type. This method will mock ALL methods that match the supplied name regardless of parameter types and signature. If this is the case you should fall-back on using the createMock(Class, Method...) method instead.

        With this method you can specify where the class hierarchy the methods are located. This is useful in, for example, situations where class A extends B and both have a method called "mockMe" (A overrides B's mockMe method) and you like to specify the only the "mockMe" method in B should be mocked. "mockMe" in A should be left intact. In this case you should do:

         A tested = createPartialMockStrict(A.class, B.class, "mockMe");
         
        Type Parameters:
        T - The type of the mock.
        Parameters:
        type - The type that'll be used to create a mock instance.
        where - Where in the class hierarchy the methods resides.
        methodNames - The names of the methods that should be mocked. If null, then this method will have the same effect as just calling createMock(Class, Method...) with the second parameter as new Method[0] (i.e. all methods in that class will be mocked).
        Returns:
        A mock object of type .
      • createNicePartialMock

        public static <T> T createNicePartialMock​(Class<T> type,
                                                  String... methodNames)
        A utility method that may be used to nicely mock several methods in an easy way (by just passing in the method names of the method you wish to mock). Note that you cannot uniquely specify a method to mock using this method if there are several methods with the same name in type. This method will mock ALL methods that match the supplied name regardless of parameter types and signature. If this is the case you should fall-back on using the createMock(Class, Method...) method instead.
        Type Parameters:
        T - The type of the mock.
        Parameters:
        type - The type that'll be used to create a mock instance.
        methodNames - The names of the methods that should be mocked. If null, then this method will have the same effect as just calling createMock(Class, Method...) with the second parameter as new Method[0] (i.e. all methods in that class will be mocked).
        Returns:
        A mock object of type .
      • createNicePartialMock

        public static <T> T createNicePartialMock​(Class<T> type,
                                                  Class<? super T> where,
                                                  String... methodNames)
        A utility method that may be used to nicely mock several methods in an easy way (by just passing in the method names of the method you wish to mock). Note that you cannot uniquely specify a method to mock using this method if there are several methods with the same name in type. This method will mock ALL methods that match the supplied name regardless of parameter types and signature. If this is the case you should fall-back on using the createMock(Class, Method...) method instead.

        With this method you can specify where the class hierarchy the methods are located. This is useful in, for example, situations where class A extends B and both have a method called "mockMe" (A overrides B's mockMe method) and you like to specify the only the "mockMe" method in B should be mocked. "mockMe" in A should be left intact. In this case you should do:

         A tested = createPartialMockNice(A.class, B.class, "mockMe");
         
        Type Parameters:
        T - The type of the mock.
        Parameters:
        type - The type that'll be used to create a mock instance.
        where - Where in the class hierarchy the methods resides.
        methodNames - The names of the methods that should be mocked. If null, then this method will have the same effect as just calling createMock(Class, Method...) with the second parameter as new Method[0] (i.e. all methods in that class will be mocked).
        Returns:
        A mock object of type .
      • createPartialMockAndInvokeDefaultConstructor

        public static <T> T createPartialMockAndInvokeDefaultConstructor​(Class<T> type,
                                                                         String... methodNames)
                                                                  throws Exception
        A utility method that may be used to mock several methods in an easy way (by just passing in the method names of the method you wish to mock). The mock object created will support mocking of final methods and invokes the default constructor (even if it's private).
        Type Parameters:
        T - the type of the mock object
        Parameters:
        type - the type of the mock object
        methodNames - The names of the methods that should be mocked. If null, then this method will have the same effect as just calling createMock(Class, Method...) with the second parameter as new Method[0] (i.e. all methods in that class will be mocked).
        Returns:
        the mock object.
        Throws:
        Exception
      • createNicePartialMockAndInvokeDefaultConstructor

        public static <T> T createNicePartialMockAndInvokeDefaultConstructor​(Class<T> type,
                                                                             String... methodNames)
                                                                      throws Exception
        A utility method that may be used to nicely mock several methods in an easy way (by just passing in the method names of the method you wish to mock). The mock object created will support mocking of final methods and invokes the default constructor (even if it's private).
        Type Parameters:
        T - the type of the mock object
        Parameters:
        type - the type of the mock object
        methodNames - The names of the methods that should be mocked. If null, then this method will have the same effect as just calling createMock(Class, Method...) with the second parameter as new Method[0] (i.e. all methods in that class will be mocked).
        Returns:
        the mock object.
        Throws:
        Exception
      • createStrictPartialMockAndInvokeDefaultConstructor

        public static <T> T createStrictPartialMockAndInvokeDefaultConstructor​(Class<T> type,
                                                                               String... methodNames)
                                                                        throws Exception
        A utility method that may be used to strictly mock several methods in an easy way (by just passing in the method names of the method you wish to mock). The mock object created will support mocking of final methods and invokes the default constructor (even if it's private).
        Type Parameters:
        T - the type of the mock object
        Parameters:
        type - the type of the mock object
        methodNames - The names of the methods that should be mocked. If null, then this method will have the same effect as just calling createMock(Class, Method...) with the second parameter as new Method[0] (i.e. all methods in that class will be mocked).
        Returns:
        the mock object.
        Throws:
        Exception
      • createPartialMock

        public static <T> T createPartialMock​(Class<T> type,
                                              String[] methodNames,
                                              Object... constructorArguments)
        A utility method that may be used to mock several methods in an easy way (by just passing in the method names of the method you wish to mock). The mock object created will support mocking of final and native methods and invokes a specific constructor based on the supplied argument values.
        Type Parameters:
        T - the type of the mock object
        Parameters:
        type - the type of the mock object
        methodNames - The names of the methods that should be mocked. If null, then this method will have the same effect as just calling createMock(Class, Method...) with the second parameter as new Method[0] (i.e. all methods in that class will be mocked).
        constructorArguments - The constructor arguments that will be used to invoke a certain constructor. (optional)
        Returns:
        the mock object.
      • createStrictPartialMock

        public static <T> T createStrictPartialMock​(Class<T> type,
                                                    String[] methodNames,
                                                    Object... constructorArguments)
        * A utility method that may be used to strictly mock several methods in an easy way (by just passing in the method names of the method you wish to mock). The mock object created will support mocking of final and native methods and invokes a specific constructor based on the supplied argument values.
        Type Parameters:
        T - the type of the mock object
        Parameters:
        type - the type of the mock object
        methodNames - The names of the methods that should be mocked. If null, then this method will have the same effect as just calling createMock(Class, Method...) with the second parameter as new Method[0] (i.e. all methods in that class will be mocked).
        constructorArguments - The constructor arguments that will be used to invoke a certain constructor. (optional)
        Returns:
        the mock object.
      • createNicePartialMock

        public static <T> T createNicePartialMock​(Class<T> type,
                                                  String[] methodNames,
                                                  Object... constructorArguments)
        * A utility method that may be used to nicely mock several methods in an easy way (by just passing in the method names of the method you wish to mock). The mock object created will support mocking of final and native methods and invokes a specific constructor based on the supplied argument values.
        Type Parameters:
        T - the type of the mock object
        Parameters:
        type - the type of the mock object
        methodNames - The names of the methods that should be mocked. If null, then this method will have the same effect as just calling createMock(Class, Method...) with the second parameter as new Method[0] (i.e. all methods in that class will be mocked).
        constructorArguments - The constructor arguments that will be used to invoke a certain constructor. (optional)
        Returns:
        the mock object.
      • createPartialMock

        public static <T> T createPartialMock​(Class<T> type,
                                              String methodName,
                                              Class<?>[] methodParameterTypes,
                                              Object... constructorArguments)
        A utility method that may be used to mock several methods in an easy way (by just passing in the method names of the method you wish to mock). Use this to handle overloaded methods. The mock object created will support mocking of final and native methods and invokes a specific constructor based on the supplied argument values.
        Type Parameters:
        T - the type of the mock object
        Parameters:
        type - the type of the mock object
        methodName - The names of the methods that should be mocked. If null, then this method will have the same effect as just calling createMock(Class, Method...) with the second parameter as new Method[0] (i.e. all methods in that class will be mocked).
        methodParameterTypes - Parameter types that defines the method. Note that this is only needed to separate overloaded methods.
        constructorArguments - The constructor arguments that will be used to invoke a certain constructor. (optional)
        Returns:
        the mock object.
      • createStrictPartialMock

        public static <T> T createStrictPartialMock​(Class<T> type,
                                                    String methodName,
                                                    Class<?>[] methodParameterTypes,
                                                    Object... constructorArguments)
        A utility method that may be used to strictly mock several methods in an easy way (by just passing in the method names of the method you wish to mock). Use this to handle overloaded methods. The mock object created will support mocking of final and native methods and invokes a specific constructor based on the supplied argument values.
        Type Parameters:
        T - the type of the mock object
        Parameters:
        type - the type of the mock object
        methodName - The names of the methods that should be mocked. If null, then this method will have the same effect as just calling createMock(Class, Method...) with the second parameter as new Method[0] (i.e. all methods in that class will be mocked).
        methodParameterTypes - Parameter types that defines the method. Note that this is only needed to separate overloaded methods.
        constructorArguments - The constructor arguments that will be used to invoke a certain constructor. (optional)
        Returns:
        the mock object.
      • createNicePartialMock

        public static <T> T createNicePartialMock​(Class<T> type,
                                                  String methodName,
                                                  Class<?>[] methodParameterTypes,
                                                  Object... constructorArguments)
        A utility method that may be used to nicely mock several methods in an easy way (by just passing in the method names of the method you wish to mock). Use this to handle overloaded methods. The mock object created will support mocking of final and native methods and invokes a specific constructor based on the supplied argument values.
        Type Parameters:
        T - the type of the mock object
        Parameters:
        type - the type of the mock object
        methodName - The names of the methods that should be mocked. If null, then this method will have the same effect as just calling createMock(Class, Method...) with the second parameter as new Method[0] (i.e. all methods in that class will be mocked).
        methodParameterTypes - Parameter types that defines the method. Note that this is only needed to separate overloaded methods.
        constructorArguments - The constructor arguments that will be used to invoke a certain constructor. (optional)
        Returns:
        the mock object.
      • createPartialMock

        public static <T> T createPartialMock​(Class<T> type,
                                              String methodName,
                                              Class<?>[] methodParameterTypes,
                                              Object[] constructorArguments,
                                              Class<?>[] constructorParameterTypes)
        A utility method that may be used to mock several methods in an easy way (by just passing in the method names of the method you wish to mock). Use this to handle overloaded methods and overloaded constructors. The mock object created will support mocking of final and native methods and invokes a specific constructor based on the supplied argument values.
        Type Parameters:
        T - the type of the mock object
        Parameters:
        type - the type of the mock object
        methodName - The names of the methods that should be mocked. If null, then this method will have the same effect as just calling createMock(Class, Method...) with the second parameter as new Method[0] (i.e. all methods in that class will be mocked).
        methodParameterTypes - Parameter types that defines the method. Note that this is only needed to separate overloaded methods.
        constructorArguments - The constructor arguments that will be used to invoke a certain constructor.
        constructorParameterTypes - Parameter types that defines the constructor that should be invoked. Note that this is only needed to separate overloaded constructors.
        Returns:
        the mock object.
      • createStrictPartialMock

        public static <T> T createStrictPartialMock​(Class<T> type,
                                                    String methodName,
                                                    Class<?>[] methodParameterTypes,
                                                    Object[] constructorArguments,
                                                    Class<?>[] constructorParameterTypes)
        A utility method that may be used to strictly mock several methods in an easy way (by just passing in the method names of the method you wish to mock). Use this to handle overloaded methods and overloaded constructors. The mock object created will support mocking of final and native methods and invokes a specific constructor based on the supplied argument values.
        Type Parameters:
        T - the type of the mock object
        Parameters:
        type - the type of the mock object
        methodName - The names of the methods that should be mocked. If null, then this method will have the same effect as just calling createMock(Class, Method...) with the second parameter as new Method[0] (i.e. all methods in that class will be mocked).
        methodParameterTypes - Parameter types that defines the method. Note that this is only needed to separate overloaded methods.
        constructorArguments - The constructor arguments that will be used to invoke a certain constructor.
        constructorParameterTypes - Parameter types that defines the constructor that should be invoked. Note that this is only needed to separate overloaded constructors.
        Returns:
        the mock object.
      • createNicePartialMock

        public static <T> T createNicePartialMock​(Class<T> type,
                                                  String methodName,
                                                  Class<?>[] methodParameterTypes,
                                                  Object[] constructorArguments,
                                                  Class<?>[] constructorParameterTypes)
        A utility method that may be used to nicely mock several methods in an easy way (by just passing in the method names of the method you wish to mock). Use this to handle overloaded methods and overloaded constructors. The mock object created will support mocking of final and native methods and invokes a specific constructor based on the supplied argument values.
        Type Parameters:
        T - the type of the mock object
        Parameters:
        type - the type of the mock object
        methodName - The names of the methods that should be mocked. If null, then this method will have the same effect as just calling createMock(Class, Method...) with the second parameter as new Method[0] (i.e. all methods in that class will be mocked).
        methodParameterTypes - Parameter types that defines the method. Note that this is only needed to separate overloaded methods.
        constructorArguments - The constructor arguments that will be used to invoke a certain constructor.
        constructorParameterTypes - Parameter types that defines the constructor that should be invoked. Note that this is only needed to separate overloaded constructors.
        Returns:
        the mock object.
      • expectPrivate

        public static <T> org.easymock.IExpectationSetters<T> expectPrivate​(Class<?> clazz,
                                                                            Method method,
                                                                            Object... arguments)
                                                                     throws Exception
        Used to specify expectations on private static methods. If possible use variant with only method name.
        Throws:
        Exception
      • expectPrivate

        public static <T> org.easymock.IExpectationSetters<T> expectPrivate​(Object instance,
                                                                            Method method,
                                                                            Object... arguments)
                                                                     throws Exception
        Used to specify expectations on private methods. If possible use variant with only method name.
        Throws:
        Exception
      • expectPrivate

        public static <T> org.easymock.IExpectationSetters<T> expectPrivate​(Object instance,
                                                                            String methodName,
                                                                            Class<?>[] parameterTypes,
                                                                            Object... arguments)
                                                                     throws Exception
        Used to specify expectations on private methods. Use this method to handle overloaded methods.
        Throws:
        Exception
      • expectPrivate

        public static <T> org.easymock.IExpectationSetters<T> expectPrivate​(Object instance,
                                                                            String methodName,
                                                                            Object... arguments)
                                                                     throws Exception
        Used to specify expectations on methods using the method name. Works on for example private or package private methods.
        Throws:
        Exception
      • expectPrivate

        public static <T> org.easymock.IExpectationSetters<T> expectPrivate​(Object instance,
                                                                            Object... arguments)
                                                                     throws Exception
        Used to specify expectations on methods without specifying a method name. Works on for example private or package private methods. PowerMock tries to find a unique method to expect based on the argument parameters. If PowerMock is unable to locate a unique method you need to revert to using expectPrivate(Object, String, Object...).
        Throws:
        Exception
      • expectPrivate

        public static <T> org.easymock.IExpectationSetters<T> expectPrivate​(Object instance,
                                                                            String methodName,
                                                                            Class<?> where,
                                                                            Class<?>[] parameterTypes,
                                                                            Object... arguments)
                                                                     throws Exception
        Used to specify expectations on methods using the method name at a specific place in the class hierarchy (specified by the where parameter). Works on for example private or package private methods.

        Use this for overloaded methods.

        Throws:
        Exception
      • expectPrivate

        public static <T> org.easymock.IExpectationSetters<T> expectPrivate​(Object instance,
                                                                            String methodName,
                                                                            Class<?> where,
                                                                            Object... arguments)
                                                                     throws Exception
        Used to specify expectations on methods using the method name at a specific place in the class hierarchy (specified by the where parameter). Works on for example private or package private methods.
        Throws:
        Exception
      • expectLastCall

        public static org.easymock.IExpectationSetters<Object> expectLastCall()
        This method just delegates to EasyMock class extensions EasyMock.expectLastCall() method.
        Returns:
        The expectation setter.
        See Also:
        EasyMock.expectLastCall()
      • niceReplayAndVerify

        public static void niceReplayAndVerify()
        Sometimes it is useful to allow replay and verify on non-mocks. For example when using partial mocking in some tests and no mocking in other test-methods, but using the same setUp and tearDown.
      • replayAll

        public static void replayAll​(Object... additionalMocks)
        Replay all classes and mock objects known by PowerMock. This includes all classes that are prepared for test using the PrepareForTest or PrepareOnlyThisForTest annotations and all classes that have had their static initializers removed by using the SuppressStaticInitializationFor annotation. It also includes all mock instances created by PowerMock such as those created or used by createMock(Class, Method...), mockStatic(Class, Method...), expectNew(Class, Object...), createPartialMock(Class, String...) etc.

        To make it easy to pass in additional mocks not created by the PowerMock API you can optionally specify them as additionalMocks . These are typically those mock objects you have created using pure EasyMock or EasyMock class extensions. No additional mocks needs to be specified if you're only using PowerMock API methods.

        Note that the additionalMocks are also automatically verified when invoking the verifyAll() method.

        Parameters:
        additionalMocks - Mocks not created by the PowerMock API. These are typically those mock objects you have created using pure EasyMock or EasyMock class extensions.
      • resetAll

        public static void resetAll​(Object... additionalMocks)
        Reset all classes and mock objects known by PowerMock. This includes all classes that are prepared for test using the PrepareForTest or PrepareOnlyThisForTest annotations and all classes that have had their static initializers removed by using the SuppressStaticInitializationFor annotation. It also includes all mock instances created by PowerMock such as those created or used by createMock(Class, Method...), mockStatic(Class, Method...), expectNew(Class, Object...), createPartialMock(Class, String...) etc.

        To make it easy to pass in additional mocks not created by the PowerMock API you can optionally specify them as additionalMocks . These are typically those mock objects you have created using pure EasyMock or EasyMock class extensions. No additional mocks needs to be specified if you're only using PowerMock API methods.

        Parameters:
        additionalMocks - Mocks not created by the PowerMock API. These are typically those mock objects you have created using pure EasyMock or EasyMock class extensions.
      • reset

        public static void reset​(Class<?>... classMocks)
        Reset a list of class mocks.
      • reset

        public static void reset​(Object... mocks)
        Reset a list of mock objects or classes.
      • replay

        public static void replay​(Object... mocks)
        Switches the mocks or classes to replay mode. Note that you must use this method when using PowerMock!
        Parameters:
        mocks - mock objects or classes loaded by PowerMock.
        Throws:
        Exception - If something unexpected goes wrong.
      • verify

        public static void verify​(Object... objects)
        Switches the mocks or classes to verify mode. Note that you must use this method when using PowerMock!
        Parameters:
        objects - mock objects or classes loaded by PowerMock.
      • createMockAndExpectNew

        public static <T> T createMockAndExpectNew​(Class<T> type,
                                                   Object... arguments)
                                            throws Exception
        Convenience method for createMock followed by expectNew.
        Parameters:
        type - The class that should be mocked.
        arguments - The constructor arguments.
        Returns:
        A mock object of the same type as the mock.
        Throws:
        Exception
      • createMockAndExpectNew

        public static <T> T createMockAndExpectNew​(Class<T> type,
                                                   Class<?>[] parameterTypes,
                                                   Object... arguments)
                                            throws Exception
        Convenience method for createMock followed by expectNew when PowerMock cannot determine which constructor to use automatically. This happens when you have one constructor taking a primitive type and another one taking the wrapper type of the primitive. For example int and Integer.
        Parameters:
        type - The class that should be mocked.
        parameterTypes - The constructor parameter types.
        arguments - The constructor arguments.
        Returns:
        A mock object of the same type as the mock.
        Throws:
        Exception
      • createNiceMockAndExpectNew

        public static <T> T createNiceMockAndExpectNew​(Class<T> type,
                                                       Object... arguments)
                                                throws Exception
        Convenience method for createNiceMock followed by expectNew.
        Parameters:
        type - The class that should be mocked.
        arguments - The constructor arguments.
        Returns:
        A mock object of the same type as the mock.
        Throws:
        Exception
      • createNiceMockAndExpectNew

        public static <T> T createNiceMockAndExpectNew​(Class<T> type,
                                                       Class<?>[] parameterTypes,
                                                       Object... arguments)
                                                throws Exception
        Convenience method for createNiceMock followed by expectNew when PowerMock cannot determine which constructor to use automatically. This happens when you have one constructor taking a primitive type and another one taking the wrapper type of the primitive. For example int and Integer.
        Parameters:
        type - The class that should be mocked.
        parameterTypes - The constructor parameter types.
        arguments - The constructor arguments.
        Returns:
        A mock object of the same type as the mock.
        Throws:
        Exception
      • createStrictMockAndExpectNew

        public static <T> T createStrictMockAndExpectNew​(Class<T> type,
                                                         Object... arguments)
                                                  throws Exception
        Convenience method for createStrictMock followed by expectNew.
        Parameters:
        type - The class that should be mocked.
        arguments - The constructor arguments.
        Returns:
        A mock object of the same type as the mock.
        Throws:
        Exception
      • createStrictMockAndExpectNew

        public static <T> T createStrictMockAndExpectNew​(Class<T> type,
                                                         Class<?>[] parameterTypes,
                                                         Object... arguments)
                                                  throws Exception
        Convenience method for createStrictMock followed by expectNew when PowerMock cannot determine which constructor to use automatically. This happens when you have one constructor taking a primitive type and another one taking the wrapper type of the primitive. For example int and Integer.
        Parameters:
        type - The class that should be mocked.
        parameterTypes - The constructor parameter types.
        arguments - The constructor arguments.
        Returns:
        A mock object of the same type as the mock.
        Throws:
        Exception
      • expectNew

        public static <T> org.easymock.IExpectationSetters<T> expectNew​(Class<T> type,
                                                                        Class<?>[] parameterTypes,
                                                                        Object... arguments)
                                                                 throws Exception
        Allows specifying expectations on new invocations. For example you might want to throw an exception or return a mock. Note that you must replay the class when using this method since this behavior is part of the class mock.

        Use this method when you need to specify parameter types for the constructor when PowerMock cannot determine which constructor to use automatically. In most cases you should use expectNew(Class, Object...) instead.

        Throws:
        Exception
      • expectNew

        public static <T> org.easymock.IExpectationSetters<T> expectNew​(Class<T> type,
                                                                        Object... arguments)
                                                                 throws Exception
        Allows specifying expectations on new invocations. For example you might want to throw an exception or return a mock. Note that you must replay the class when using this method since this behavior is part of the class mock.
        Throws:
        Exception
      • expectNew

        public static <T> org.easymock.IExpectationSetters<T> expectNew​(String fullyQualifiedName,
                                                                        Object... arguments)
                                                                 throws Exception
        Allows specifying expectations on new invocations for private member (inner) classes, local or anonymous classes. For example you might want to throw an exception or return a mock. Note that you must replay the class when using this method since this behavior is part of the class mock.
        Parameters:
        fullyQualifiedName - The fully-qualified name of the inner/local/anonymous type to expect.
        arguments - Optional number of arguments.
        Throws:
        Exception
      • expectStrictNew

        public static <T> org.easymock.IExpectationSetters<T> expectStrictNew​(Class<T> type,
                                                                              Object... arguments)
                                                                       throws Exception
        Allows specifying expectations on new invocations. For example you might want to throw an exception or return a mock.

        This method checks the order of constructor invocations.

        Note that you must replay the class when using this method since this behavior is part of the class mock.

        Throws:
        Exception
      • expectStrictNew

        public static <T> org.easymock.IExpectationSetters<T> expectStrictNew​(Class<T> type,
                                                                              Class<?>[] parameterTypes,
                                                                              Object... arguments)
                                                                       throws Exception
        Allows specifying expectations on new invocations. For example you might want to throw an exception or return a mock. Note that you must replay the class when using this method since this behavior is part of the class mock.

        This method checks the order of constructor invocations.

        Use this method when you need to specify parameter types for the constructor when PowerMock cannot determine which constructor to use automatically. In most cases you should use expectNew(Class, Object...) instead.

        Throws:
        Exception
      • expectNiceNew

        public static <T> org.easymock.IExpectationSetters<T> expectNiceNew​(Class<T> type,
                                                                            Object... arguments)
                                                                     throws Exception
        Allows specifying expectations on new invocations. For example you might want to throw an exception or return a mock.

        This method allows any number of calls to a new constructor without throwing an exception.

        Note that you must replay the class when using this method since this behavior is part of the class mock.

        Throws:
        Exception
      • expectNiceNew

        public static <T> org.easymock.IExpectationSetters<T> expectNiceNew​(Class<T> type,
                                                                            Class<?>[] parameterTypes,
                                                                            Object... arguments)
                                                                     throws Exception
        Allows specifying expectations on new invocations. For example you might want to throw an exception or return a mock. Note that you must replay the class when using this method since this behavior is part of the class mock.

        This method allows any number of calls to a new constructor without throwing an exception.

        Use this method when you need to specify parameter types for the constructor when PowerMock cannot determine which constructor to use automatically. In most cases you should use expectNew(Class, Object...) instead.

        Throws:
        Exception
      • suppressSpecificConstructor

        public static void suppressSpecificConstructor​(Class<?> clazz,
                                                       Class<?>... parameterTypes)
        Deprecated.
        This method can be used to suppress the code in a specific constructor.
        Parameters:
        clazz - The class where the constructor is located.
        parameterTypes - The parameter types of the constructor to suppress.
      • suppressConstructor

        public static void suppressConstructor​(Class<?>... classes)
        Deprecated.
        Suppress all constructors in the given class and it's super classes.
        Parameters:
        classes - The classes whose constructors will be suppressed.
      • suppressConstructor

        public static void suppressConstructor​(Class<?> clazz,
                                               boolean excludePrivateConstructors)
        Deprecated.
        Suppress all constructors in the given class.
        Parameters:
        clazz - The classes whose constructors will be suppressed.
        excludePrivateConstructors - optionally keep code in private constructors
      • suppressField

        public static void suppressField​(Field... fields)
        Deprecated.
        Suppress specific fields. This works on both instance methods and static methods. Note that replay and verify are not needed as this is not part of a mock behavior.
      • suppressField

        public static void suppressField​(Class<?> clazz,
                                         String... fieldNames)
        Deprecated.
        Suppress multiple methods for a class.
        Parameters:
        clazz - The class whose methods will be suppressed.
        fieldNames - The names of the methods that'll be suppressed. If field names are empty, all fields in the supplied class will be suppressed.
      • suppressMethod

        public static void suppressMethod​(Method... methods)
        Deprecated.
        Suppress specific method calls on all types containing this method. This works on both instance methods and static methods. Note that replay and verify are not needed as this is not part of a mock behavior.
      • suppressMethod

        public static void suppressMethod​(Class<?> cls,
                                          Class<?>... additionalClasses)
        Deprecated.
        Suppress all methods for these classes.
        Parameters:
        cls - The first class whose methods will be suppressed.
        additionalClasses - Additional classes whose methods will be suppressed.
      • suppressMethod

        public static void suppressMethod​(Class<?>[] classes)
        Deprecated.
        Suppress all methods for these classes.
        Parameters:
        classes - Classes whose methods will be suppressed.
      • suppressMethod

        public static void suppressMethod​(Class<?> clazz,
                                          String methodName,
                                          String... additionalMethodNames)
        Deprecated.
        Suppress multiple methods for a class.
        Parameters:
        clazz - The class whose methods will be suppressed.
        methodName - The first method to be suppress in class clazz.
        additionalMethodNames - Additional methods to suppress in class clazz.
      • suppressMethod

        public static void suppressMethod​(Class<?> clazz,
                                          String[] methodNames)
        Deprecated.
        Suppress multiple methods for a class.
        Parameters:
        clazz - The class whose methods will be suppressed.
        methodNames - Methods to suppress in class clazz.
      • suppressMethod

        public static void suppressMethod​(Class<?> clazz,
                                          boolean excludePrivateMethods)
        Deprecated.
        Suppress all methods for this class.
        Parameters:
        clazz - The class which methods will be suppressed.
        excludePrivateMethods - optionally not suppress private methods
      • suppressMethod

        public static void suppressMethod​(Class<?> clazz,
                                          String methodName,
                                          Class<?>[] parameterTypes)
        Deprecated.
        Suppress a specific method call. Use this for overloaded methods.