se.mockachino
Class Mockachino

java.lang.Object
  extended by se.mockachino.Mockachino

public class Mockachino
extends java.lang.Object

This is the main entry point of all your mocking needs. All operations delegates to a singleton MockUtil.

For most use cases, using this singleton is the most convenient approach, but are also free to use a MockUtil object directly.

Consider statically import these classes to reduce verbosity of tests:

 import static se.mockachino.Mockachino.*;
 import static se.mockachino.matchers.Matchers.*;
 import static se.mockachino.Settings.*;
 

See MatchersBase for available matchers and see Matchers for useful matcher shortcuts.

See Settings for how to mock classes with various settings.


Field Summary
static MockPoint BIG_BANG
           
static MockPoint BIG_CRUNCH
           
static CallHandler DEEP_MOCK
           
static CallHandler DEFAULT_VALUES
           
static PrimitiveInvocationHandler PRIMITIVE_VALUES
           
 
Constructor Summary
Mockachino()
           
 
Method Summary
static BetweenVerifyContext after(MockPoint start)
          Verify that calls happened at or after a point in time.
static BetweenVerifyContext before(MockPoint end)
          Verify that calls happened at or before a point in time.
static BetweenVerifyContext between(MockPoint start, MockPoint end)
          Verify that calls happened between (inclusive) two points in time.
static boolean canMock(java.lang.Class clazz)
          Checks if a class is mockable by Mockachino.
static MockPoint getCurrentPoint()
          Gets the current point on time on the call history.
static
<T> MockData<T>
getData(T mock)
          Get the metadata for mock.
static
<T> T
mock( type)
          Creates a new mock with a default handler and default settings.
static
<T> T
mock( type, MockSettings settings)
          Creates a new mock with specified settings.
static
<T> T
mock(java.lang.Class<T> clazz)
          Creates a new mock with a default handler and default settings.
static
<T> T
mock(java.lang.Class<T> clazz, MockSettings settings)
          Creates a new mock with specified settings.
static java.lang.Object mockType(java.lang.reflect.Type type, MockSettings mockSettings)
           
static SimpleAlias newAlias()
           
static OrderingContext newOrdering()
          Creates a new ordering context which is used to verify method calls in order.
static ObserverAdder observeWith(CallHandler observer)
          Adds an observer to a specific method call.
static void setupMocks(java.lang.Object obj)
           
static
<T> T
spy(T impl)
          Creates a new mock with class impl.getClass() with the impl as a default handler.
static
<T> T
spy(T impl, MockSettings settings)
          Creates a new mock with class impl.getClass() with the impl as a default handler, and the specified settings.
static Stubber stubAnswer(CallHandler answer)
          Stubs a method call with a specific answer strategy.
static Stubber stubReturn(java.lang.Object... returnValues)
           
static Stubber stubReturn(java.lang.Object returnValue)
          Stubs a method call to return a specific value.
static Stubber stubThrow(java.lang.Throwable e)
          Stubs a method call to throw an exception.
static VerifyRangeStart verifyAtLeast(int min)
          Verifies that a method call is called at least a specific number of times.
static VerifyRangeStart verifyAtMost(int max)
          Verifies that a method call is called at most a specific number of times.
static VerifyRangeStart verifyExactly(int count)
          Verifies that a method call is called an exact number of times.
static VerifyRangeStart verifyNever()
          Verifies that a method call is never called.
static VerifyRangeStart verifyOnce()
          Verifies that a method call is only called exactly once.
static VerifyRangeStart verifyRange(int min, int max)
          Verifies that a method call is called between min and max times, inclusive.
static
<T>
when(T mockInvocation)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PRIMITIVE_VALUES

public static final PrimitiveInvocationHandler PRIMITIVE_VALUES

DEFAULT_VALUES

public static final CallHandler DEFAULT_VALUES

DEEP_MOCK

public static final CallHandler DEEP_MOCK

BIG_BANG

public static final MockPoint BIG_BANG

BIG_CRUNCH

public static final MockPoint BIG_CRUNCH
Constructor Detail

Mockachino

public Mockachino()
Method Detail

mock

public static <T> T mock(java.lang.Class<T> clazz)
Creates a new mock with a default handler and default settings.

Parameters:
clazz - the interface or class to mock
Returns:
a mock object of the same class

mock

public static <T> T mock( type)
Creates a new mock with a default handler and default settings.

Parameters:
type - the type literal of the interface or class to mock
Returns:
a mock object of the same class

mock

public static <T> T mock(java.lang.Class<T> clazz,
                         MockSettings settings)
Creates a new mock with specified settings.

Parameters:
clazz - the interface or class to mock
Returns:
a mock object of the same class

mock

public static <T> T mock( type,
                         MockSettings settings)
Creates a new mock with specified settings.

Parameters:
type - the type literal of the interface or class to mock
Returns:
a mock object of the same class

spy

public static <T> T spy(T impl)
Creates a new mock with class impl.getClass() with the impl as a default handler. Equivalent to mock(impl.getClass(), Settings.spyOn(impl));

Parameters:
impl - the object to spy on
Returns:
a mock object of the same class as impl

spy

public static <T> T spy(T impl,
                        MockSettings settings)
Creates a new mock with class impl.getClass() with the impl as a default handler, and the specified settings.

Equivalent to mock(impl.getClass(), settings.spyOn(impl));

Parameters:
impl - the object to spy on
Returns:
a mock object of the same class as impl

newOrdering

public static OrderingContext newOrdering()
Creates a new ordering context which is used to verify method calls in order. Ordering contexts are completely independent of each other.

Typical usage:

 OrderingContext context = Mockachino.newOrdering();
 context.verifyAtLeast(3).on(mock).method();
 

Returns:
the new ordering context

verifyRange

public static VerifyRangeStart verifyRange(int min,
                                           int max)
Verifies that a method call is called between min and max times, inclusive.

Typical usage:

 Mockachino.verifyRange(1, 2).on(mock).method();
 

Parameters:
min - minimum amount of matching method calls
max - maximum amount of matching method calls
Returns:
a verifier

verifyExactly

public static VerifyRangeStart verifyExactly(int count)
Verifies that a method call is called an exact number of times.

Typical usage:

 Mockachino.verifyExactly(3).on(mock).method();
 

Parameters:
count - number of times the method should be called
Returns:
a verifier

verifyNever

public static VerifyRangeStart verifyNever()
Verifies that a method call is never called.

Typical usage:

 Mockachino.verifyNever().on(mock).method();
 

Returns:
a verifier

verifyOnce

public static VerifyRangeStart verifyOnce()
Verifies that a method call is only called exactly once.

Typical usage:

 Mockachino.verifyOnce().on(mock).method();
 

Returns:
a verifier

verifyAtLeast

public static VerifyRangeStart verifyAtLeast(int min)
Verifies that a method call is called at least a specific number of times.

Typical usage:

 Mockachino.verifyAtLeast(3).on(mock).method();
 

Parameters:
min - number of times the method should be called
Returns:
a verifier

verifyAtMost

public static VerifyRangeStart verifyAtMost(int max)
Verifies that a method call is called at most a specific number of times.

Typical usage:

 Mockachino.verifyAtMost(3).on(mock).method();
 

Parameters:
max - number of times the method should be called
Returns:
a verifier

stubThrow

public static Stubber stubThrow(java.lang.Throwable e)
Stubs a method call to throw an exception.

Typical usage:

 Mockachino.stubThrow(myException).on(mock).method();
 

Parameters:
e - the exception to throw
Returns:
a stubber

stubReturn

public static Stubber stubReturn(java.lang.Object returnValue)
Stubs a method call to return a specific value.

Typical usage:

 Mockachino.stubReturn(value).on(mock).method();
 

Note that the type of the value must match the return type of the method call. This is checked at runtime and will throw a UsageError if they don't match.

Parameters:
returnValue - the returnValue to return when the method is called.
Returns:
a stubber

stubReturn

public static Stubber stubReturn(java.lang.Object... returnValues)

stubAnswer

public static Stubber stubAnswer(CallHandler answer)
Stubs a method call with a specific answer strategy.

Typical usage:

 Mockachino.stubAnswer(answer).on(mock).method();
 

All matching method calls will invoke the getValue()-method on the answer-object.

Parameters:
answer - the answer to use
Returns:
a stubber

observeWith

public static ObserverAdder observeWith(CallHandler observer)
Adds an observer to a specific method call. The observer will get a callback every time the method is called and the arguments match.

Typical usage:

 Mockachino.observeWith(observer).on(mock).method();
 

Parameters:
observer - the observer to use
Returns:

getCurrentPoint

public static MockPoint getCurrentPoint()
Gets the current point on time on the call history. It can be useful to run between calls to the code under test, to l ater verify that calls happened during specific times.

Returns:
the mock point

between

public static BetweenVerifyContext between(MockPoint start,
                                           MockPoint end)
Verify that calls happened between (inclusive) two points in time.

Typical usage:

 Mockachino.between(start, end).verifyAtLeast(1).on(mock).method();
 

Parameters:
start -
end -
Returns:
a verifying context

after

public static BetweenVerifyContext after(MockPoint start)
Verify that calls happened at or after a point in time.

Typical usage:

 Mockachino.after(start).verifyAtLeast(1).on(mock).method();
 

Parameters:
start -
Returns:
a verifying context

before

public static BetweenVerifyContext before(MockPoint end)
Verify that calls happened at or before a point in time.

Typical usage:

 Mockachino.before(end).verifyAtLeast(1).on(mock).method();
 

Parameters:
end -
Returns:
a verifying context

canMock

public static boolean canMock(java.lang.Class clazz)
Checks if a class is mockable by Mockachino.

Parameters:
clazz -
Returns:
true if the class is mockable, otherwise false.

setupMocks

public static void setupMocks(java.lang.Object obj)

getData

public static <T> MockData<T> getData(T mock)
Get the metadata for mock.

This can be used both for resetting mocks, and inspecting calls on the mock.

Parameters:
mock - the mock object
Returns:
the mock metadata

newAlias

public static SimpleAlias newAlias()

when

public static <T>  when(T mockInvocation)

mockType

public static java.lang.Object mockType(java.lang.reflect.Type type,
                                        MockSettings mockSettings)