org.powermock.api.mockito
Class PowerMockito

java.lang.Object
  extended by org.powermock.api.mockito.PowerMockito

public class PowerMockito
extends Object

PowerMockito extends Mockito functionality with several new features such as mocking static and private methods, mocking new instances and more. Use PowerMock instead of Mockito where applicable.


Constructor Summary
PowerMockito()
           
 
Method Summary
static
<T> T
mock(Class<T> type)
          Creates a mock object that supports mocking of final and native methods.
static
<T> T
mock(Class<T> type, Method... methods)
          Creates a mock object that supports mocking of final and native methods.
static void mockStatic(Class<?> type)
          Enable static mocking for all methods of a class.
static void mockStatic(Class<?> type, Method... methods)
          Enable static mocking for a class.
static
<T> T
spy(T object)
          Spy on objects that are final or otherwise not "spyable" from normal Mockito.
static void verifyStatic(Class<?> mock)
          Verifies certain behavior happened once Alias to verifyStatic(mock, times(1)) E.g: verifyStatic(ClassWithStaticMethod.class); ClassWithStaticMethod.someStaticMethod("some arg"); Above is equivalent to: verifyStatic(ClassWithStaticMethod.class, times(1)); ClassWithStaticMethod.someStaticMethod("some arg"); Although it is possible to verify a stubbed invocation, usually it's just redundant.
static void verifyStatic(Class<?> mock, org.mockito.internal.verification.api.VerificationMode mode)
          Verifies certain behavior happened at least once / exact number of times / never.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PowerMockito

public PowerMockito()
Method Detail

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 all methods of a class.

Parameters:
type - the class to enable static mocking

mock

public static <T> T mock(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.

mock

public static <T> T mock(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.

spy

public static <T> T spy(T object)
Spy on objects that are final or otherwise not "spyable" from normal Mockito.

Type Parameters:
T - the type of the mock object
Parameters:
type - the type of the mock object
Returns:
the spy object.
See Also:
Mockito.spy(Object)

verifyStatic

public static void verifyStatic(Class<?> mock)
Verifies certain behavior happened once

Alias to verifyStatic(mock, times(1)) E.g:

 verifyStatic(ClassWithStaticMethod.class);
 ClassWithStaticMethod.someStaticMethod("some arg");
 
Above is equivalent to:
 verifyStatic(ClassWithStaticMethod.class, 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 cares what foo.bar() returns then something else breaks(often before even verify() gets executed). If your code doesn't care what get(0) returns then it should not be stubbed.

Parameters:
mock - Class mocked by PowerMock.

verifyStatic

public static void verifyStatic(Class<?> mock,
                                org.mockito.internal.verification.api.VerificationMode mode)
Verifies certain behavior happened at least once / exact number of times / never. E.g:
   verifyStatic(ClassWithStaticMethod.class, times(5));
   ClassWithStaticMethod.someStaticMethod("was called five times");
   
   verifyStatic(ClassWithStaticMethod.class, atLeast(2));
   ClassWithStaticMethod.someStaticMethod("was called at least two times");
   
   //you can use flexible argument matchers, e.g:
   verifyStatic(ClassWithStaticMethod.class, atLeastOnce());
   ClassWithStaticMethod.someMethod(<b>anyString()</b>);
 
times(1) is the default and can be omitted

Parameters:
mock - to be verified
mode - times(x), atLeastOnce() or never()


Copyright © 2007-2009. All Rights Reserved.