- java.lang.Object
-
- com.roscopeco.moxy.Moxy
-
public final class Moxy extends Object
Top-level static-imports for convenient Moxy use.
This class, along with the
Matchers
class, are the main top-level classes most users will need to interact with when using Moxy. They are designed so you can simplyimport static
and start mocking. For example:import static com.roscopeco.moxy.Moxy.*; import static com.roscopeco.moxy.matchers.Matchers.*; // ... later ... SomeClass mock = mock(SomeClass.class); when(() -> mock.someMethod(any(), eq("something"))).thenReturn("whatever"); // ... mock usage ... assertMock(() -> mock.someMethod("expected", "something").wasCalledTwice(); assertMock(() -> mock.someMethod("badfood", "something").wasNotCalled();
See
README.md
for more detailed usage of the library.- Since:
- 1.0
- Author:
- Ross Bamford <roscopeco AT gmail DOT com>
- See Also:
Matchers
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static MoxyVerifier
assertMock(InvocationRunnable invocation)
Starts verification of the mock invocation in the supplied lambda expression using the currently-set (or default)MoxyEngine
.static MoxyVerifier
assertMock(MoxyEngine engine, InvocationRunnable invocation)
Starts verification of the mock invocation in the supplied lambda expression using the specifiedMoxyEngine
.static MoxyMultiVerifier
assertMocks(InvocationRunnable invocation)
Starts verification of one or more mock invocations at the same time.static MoxyMultiVerifier
assertMocks(MoxyEngine engine, InvocationRunnable invocation)
Starts verification of one or more mock invocations at the same time.static <T> T
constructMock(MoxyEngine engine, Class<T> clz, PrintStream trace, Object... args)
Create a mock instance of the given class using the suppliedMoxyEngine
using a constructor call.static <T> T
constructMock(Class<T> clz, PrintStream trace, Object... args)
Create a mock instance of the given class using the defaultMoxyEngine
using a constructor call.static <T> T
constructMock(Class<T> clz, Object... args)
Create a mock instance of the given class using the defaultMoxyEngine
using a constructor call.static <T> T
constructSpy(MoxyEngine engine, Class<T> clz, PrintStream trace, Object... args)
Create a spy instance of the given class using the suppliedMoxyEngine
using a constructor call.static <T> T
constructSpy(Class<T> clz, PrintStream trace, Object... args)
Create a spy instance of the given class using the defaultMoxyEngine
using a constructor call.static <T> T
constructSpy(Class<T> clz, Object... args)
Create a spy instance of the given class using the defaultMoxyEngine
using a constructor call.static MoxyClassMockEngine
getMoxyClassMockEngine()
Get the currentMoxyClassMockEngine
in use by this class, or create and return a default engine if none has been set.static MoxyEngine
getMoxyEngine()
Get the currentMoxyEngine
in use by this class, or create and return a default engine if none has been set.static boolean
isMock(MoxyEngine engine, Class<?> clz)
Determines whether the supplied class is a mock class in the context of the suppliedMoxyEngine
.static boolean
isMock(MoxyEngine engine, Object obj)
Determines whether the supplied class is a mock class in the context of the suppliedMoxyEngine
.static boolean
isMock(Class<?> clz)
Determines whether the supplied class is a mock class.static boolean
isMock(Object obj)
Determines whether the supplied object is a mock instance.static <T> T
mock(MoxyEngine engine, Class<T> clz, PrintStream trace)
Create a mock instance of the given class using the specifiedMoxyEngine
.static <T> T
mock(Class<T> clz)
Create a mock instance of the given class using the currently-set (or default)MoxyEngine
.static <T> T
mock(Class<T> clz, PrintStream trace)
Create a mock instance of the given class using the currently-set (or default)MoxyEngine
.static void
mockClasses(MoxyClassMockEngine engine, Class<?>... classes)
Convert the given classes to mock classes using the suppliedMoxyClassMockEngine
.static void
mockClasses(Class<?>... classes)
Convert the given classes to mock classes using the defaultMoxyClassMockEngine
.static void
resetAllClassMocks()
Reset all class mocks created with the defaultMoxyClassMockEngine
to their original, un-mocked state.static void
resetAllClassMocks(MoxyClassMockEngine engine)
Reset all class mocks created with the suppliedMoxyClassMockEngine
to their original, un-mocked state.static void
resetClassMocks(MoxyClassMockEngine engine, Class<?>... classes)
Reset the given classes to their original, non-mock implementation, using the suppliedMoxyClassMockEngine
.static void
resetClassMocks(Class<?>... classes)
Reset the given classes to their original, non-mock implementation, using the defaultMoxyClassMockEngine
.static void
resetMock(MoxyEngine engine, Object mock)
Reset the supplied mock using the supplied engine, removing all stubbing that was previously applied.static void
resetMock(Object mock)
Reset the supplied mock, removing all stubbing that was previously applied.static void
setMoxyClassMockEngine(MoxyClassMockEngine moxyClassMockEngine)
Set theMoxyClassMockEngine
that will be used by all future calls to methods in this class.static void
setMoxyEngine(MoxyEngine moxyEngine)
Set theMoxyEngine
that will be used by all future calls to methods in this class.static <T> T
spy(MoxyEngine engine, Class<T> clz, PrintStream trace)
Create a spy instance of the given class using the specifiedMoxyEngine
.static <T> T
spy(Class<T> clz)
Create a spy instance of the given class using the defaultMoxyEngine
.static <T> T
spy(Class<T> clz, PrintStream trace)
Create a spy instance of the given class using the defaultMoxyEngine
.static <T> T
spy(T object)
Convert the given object into a spy.static MoxyVoidStubber
when(InvocationRunnable invocation)
Starts stubbing of the mock invocation in the supplied lambda expression using the currently-set (or default)MoxyEngine
.static <T> MoxyStubber<T>
when(InvocationSupplier<T> invocation)
Starts stubbing of the mock invocation in the supplied lambda expression using the currently-set (or default)MoxyEngine
.static MoxyVoidStubber
when(MoxyEngine engine, InvocationRunnable invocation)
Starts stubbing of the mock invocation in the supplied lambda expression using the specifiedMoxyEngine
.static <T> MoxyStubber<T>
when(MoxyEngine engine, InvocationSupplier<T> invocation)
Starts stubbing of the mock invocation in the supplied lambda expression using the specifiedMoxyEngine
.
-
-
-
Method Detail
-
setMoxyEngine
public static void setMoxyEngine(MoxyEngine moxyEngine)
Set the
MoxyEngine
that will be used by all future calls to methods in this class.Note: Calling this method after the current (or default) engine has been used may cause problems. If you are changing the engine, you should change it before interacting with the library in any other way (especially creating mocks).
- Parameters:
moxyEngine
- TheMoxyEngine
implementation to use.- Since:
- 1.0
-
getMoxyEngine
public static MoxyEngine getMoxyEngine()
Get the currentMoxyEngine
in use by this class, or create and return a default engine if none has been set.- Returns:
- The
MoxyEngine
currently in use. - Since:
- 1.0
-
setMoxyClassMockEngine
public static void setMoxyClassMockEngine(MoxyClassMockEngine moxyClassMockEngine)
Set the
MoxyClassMockEngine
that will be used by all future calls to methods in this class.Note: Calling this method after the current (or default) engine has been used may cause problems. If you are changing the engine, you should change it before interacting with the library in any other way (especially creating mocks).
- Parameters:
moxyClassMockEngine
- TheMoxyClassMockEngine
implementation to use.- Since:
- 1.0
-
getMoxyClassMockEngine
public static MoxyClassMockEngine getMoxyClassMockEngine()
Get the currentMoxyClassMockEngine
in use by this class, or create and return a default engine if none has been set.- Returns:
- The
MoxyClassMockEngine
currently in use. - Since:
- 1.0
-
mockClasses
public static void mockClasses(Class<?>... classes)
Convert the given classes to mock classes using the default
MoxyClassMockEngine
.when this method returns, all instances of the given classes will be converted to mocks, along with all future instances.
This is not the normal style of mocking. See
MoxyClassMockEngine
for details.- Parameters:
classes
- Classes to convert.- Since:
- 1.0
- See Also:
MoxyClassMockEngine
,MoxyClassMockEngine.mockClasses(Class...)
-
mockClasses
public static void mockClasses(MoxyClassMockEngine engine, Class<?>... classes)
Convert the given classes to mock classes using the supplied
MoxyClassMockEngine
.when this method returns, all instances of the given classes will be converted to mocks, along with all future instances.
This is not the normal style of mocking. See
MoxyClassMockEngine
for details.- Parameters:
engine
- TheMoxyClassMockEngine
to use.classes
- Classes to convert.- Since:
- 1.0
- See Also:
MoxyClassMockEngine
,MoxyClassMockEngine.mockClasses(Class...)
-
resetClassMocks
public static void resetClassMocks(Class<?>... classes)
Reset the given classes to their original, non-mock implementation, using the default
MoxyClassMockEngine
.- Parameters:
classes
- Classes to reset.- Since:
- 1.0
- See Also:
mockClasses(Class...)
,MoxyClassMockEngine
,MoxyClassMockEngine.mockClasses(Class...)
-
resetClassMocks
public static void resetClassMocks(MoxyClassMockEngine engine, Class<?>... classes)
Reset the given classes to their original, non-mock implementation, using the supplied
MoxyClassMockEngine
.- Parameters:
engine
- TheMoxyClassMockEngine
to use.classes
- Classes to reset.- Since:
- 1.0
- See Also:
mockClasses(MoxyClassMockEngine, Class...)
,MoxyClassMockEngine
,MoxyClassMockEngine.mockClasses(Class...)
-
resetAllClassMocks
public static void resetAllClassMocks()
Reset all class mocks created with the defaultMoxyClassMockEngine
to their original, un-mocked state.- Since:
- 1.0
- See Also:
resetClassMocks(Class...)
-
resetAllClassMocks
public static void resetAllClassMocks(MoxyClassMockEngine engine)
Reset all class mocks created with the suppliedMoxyClassMockEngine
to their original, un-mocked state.- Parameters:
engine
- TheMoxyClassMockEngine
to use.- Since:
- 1.0
- See Also:
resetClassMocks(MoxyClassMockEngine, Class...)
-
mock
public static <T> T mock(Class<T> clz)
Create a mock instance of the given class using the currently-set (or default)
MoxyEngine
.The mock will be created in the engine's default
ClassLoader
. For the default engine, this is the sameClassLoader
that loaded the Moxy framework.Note: This method will not call a constructor on the generated instance, which may cause problems if you plan to use #
MoxyStubber.thenCallRealMethod()
with this mock.If that is the case, see
constructSpy(Class, Object...)
.- Type Parameters:
T
- The type being mocked.- Parameters:
clz
- TheClass
to mock.- Returns:
- A new mock instance.
- Since:
- 1.0
- See Also:
mock(Class, PrintStream)
,mock(MoxyEngine, Class, PrintStream)
-
mock
public static <T> T mock(Class<T> clz, PrintStream trace)
Create a mock instance of the given class using the currently-set (or default)
MoxyEngine
.The mock will be created in the engine's default
ClassLoader
. For the default engine, this is the sameClassLoader
that loaded the Moxy framework.During creation of the mock, this method will also dump the generated bytecode, in a debugging format, to the supplied
PrintStream
.Note: This method will not call a constructor on the generated instance, which may cause problems if you plan to use #
MoxyStubber.thenCallRealMethod()
with this mock.If that is the case, see
constructSpy(Class, PrintStream, Object...)
.- Type Parameters:
T
- The type being mocked.- Parameters:
clz
- TheClass
to mock.trace
- If non-null, the resulting class will be dumped (with aTraceClassVisitor
) to the given stream.- Returns:
- A new mock instance.
- Since:
- 1.0
- See Also:
mock(Class)
,mock(MoxyEngine, Class, PrintStream)
-
mock
public static <T> T mock(MoxyEngine engine, Class<T> clz, PrintStream trace)
Create a mock instance of the given class using the specified
MoxyEngine
.The mock will be created in the engine's default
ClassLoader
. For the default engine, this is the sameClassLoader
that loaded the Moxy framework.During creation of the mock, this method will also dump the generated bytecode, in a debugging format, to the supplied
PrintStream
.Note: This method will not call a constructor on the generated instance, which may cause problems if you plan to use #
MoxyStubber.thenCallRealMethod()
with this mock.If that is the case, see
constructSpy(MoxyEngine, Class, PrintStream, Object...)
.- Type Parameters:
T
- The type being mocked.- Parameters:
engine
- TheMoxyEngine
implementation to use.clz
- TheClass
to mock.trace
- If non-null, the resulting class will be dumped (with aTraceClassVisitor
) to the given stream.- Returns:
- A new mock instance.
- Since:
- 1.0
- See Also:
mock(Class)
,mock(Class, PrintStream)
-
constructMock
public static <T> T constructMock(Class<T> clz, Object... args)
Create a mock instance of the given class using the default
MoxyEngine
using a constructor call. This method is intended for use when one or more methods will be spied (withMoxyStubber.thenCallRealMethod()
.The mock will be created in the engine's default
ClassLoader
. For the default engine, this is the sameClassLoader
that loaded the Moxy framework.This method uses a best-fit algorithm to determine which constructor to call based on the supplied arguments. Primitive types will be boxed/unboxed automatically.
- Type Parameters:
T
- The type being mocked.- Parameters:
clz
- TheClass
to spy.args
- Constructor arguments for the mock.- Returns:
- A new mock instance.
- Since:
- 1.0
- See Also:
mock(Class)
,constructMock(Class, PrintStream, Object...)
,constructMock(MoxyEngine, Class, PrintStream, Object...)
,MoxyStubber.thenCallRealMethod()
-
constructMock
public static <T> T constructMock(Class<T> clz, PrintStream trace, Object... args)
Create a mock instance of the given class using the default
MoxyEngine
using a constructor call. This method is intended for use when one or more methods will be spied (withMoxyStubber.thenCallRealMethod()
.The mock will be created in the engine's default
ClassLoader
. For the default engine, this is the sameClassLoader
that loaded the Moxy framework.During creation of the mock, this method will also dump the generated bytecode, in a debugging format, to the supplied
PrintStream
(if non-null
).This method uses a best-fit algorithm to determine which constructor to call based on the supplied arguments. Primitive types will be boxed/unboxed automatically.
- Type Parameters:
T
- The type being mocked.- Parameters:
clz
- TheClass
to spy.trace
- If non-null, the resulting class will be dumped (with aTraceClassVisitor
) to the given stream.args
- Constructor arguments for the mock.- Returns:
- A new mock instance.
- Since:
- 1.0
- See Also:
mock(Class)
,constructMock(Class, Object...)
,constructMock(MoxyEngine, Class, PrintStream, Object...)
,MoxyStubber.thenCallRealMethod()
-
constructMock
public static <T> T constructMock(MoxyEngine engine, Class<T> clz, PrintStream trace, Object... args)
Create a mock instance of the given class using the supplied
MoxyEngine
using a constructor call. This method is intended for use when one or more methods will be spied (withMoxyStubber.thenCallRealMethod()
.The mock will be created in the engine's default
ClassLoader
. For the default engine, this is the sameClassLoader
that loaded the Moxy framework.During creation of the mock, this method will also dump the generated bytecode, in a debugging format, to the supplied
PrintStream
(if non-null
).This method uses a best-fit algorithm to determine which constructor to call based on the supplied arguments. Primitive types will be boxed/unboxed automatically.
- Type Parameters:
T
- The type being mocked.- Parameters:
engine
- TheMoxyEngine
implementation to use.clz
- TheClass
to mock.trace
- If non-null, the resulting class will be dumped (with aTraceClassVisitor
) to the given stream.args
- Constructor arguments for the mock.- Returns:
- A new mock instance.
- Since:
- 1.0
- See Also:
mock(Class)
,constructMock(Class, Object...)
,constructMock(Class, PrintStream, Object...)
,MoxyStubber.thenCallRealMethod()
-
spy
public static <T> T spy(T object)
Convert the given object into a spy.
This method can be used to convert any Object into a spy. It behaves differently depending on whether the passed object is a mock, or a standard object:
If the given object is a standard object, then a new spy will be created that uses
MoxyStubber.thenDelegateTo(Object)
behind the scenes to delegate all calls to the supplied object.If the given object is a mock, then all existing stubbing will be replaced with
MoxyStubber.thenCallRealMethod()
.
It is, in the latter case, important to note that this may not be exactly the behaviour you expect. You should always convert to a spy before applying any additional stubbing.
Note that this will reset all prior stubbing on the given mock.
- Type Parameters:
T
- The type of the mock.- Parameters:
object
- The object to be spied-upon.- Returns:
- The mock, converted to a spy.
- Since:
- 1.0
- See Also:
spy(Class)
,spy(Class, PrintStream)
,spy(MoxyEngine, Class, PrintStream)
-
spy
public static <T> T spy(Class<T> clz)
Create a spy instance of the given class using the default
MoxyEngine
.The spy will be created in the engine's default
ClassLoader
. For the default engine, this is the sameClassLoader
that loaded the Moxy framework.Note: This method will not call a constructor on the generated instance, which may be more troublesome for spies than it is for mocks (as the real methods being called may rely on some state initialized by the constructor).
If you require a constructor be called, see
constructSpy(Class, Object...)
.- Type Parameters:
T
- The type being spied.- Parameters:
clz
- TheClass
to spy.- Returns:
- A new spy instance.
- Since:
- 1.0
- See Also:
spy(Class, PrintStream)
,spy(MoxyEngine, Class, PrintStream)
,spy(Object)
,constructSpy(Class, Object...)
,mock(Class)
,MoxyStubber.thenCallRealMethod()
-
spy
public static <T> T spy(Class<T> clz, PrintStream trace)
Create a spy instance of the given class using the default
MoxyEngine
.The spy will be created in the engine's default
ClassLoader
. For the default engine, this is the sameClassLoader
that loaded the Moxy framework.During creation of the spy, this method will also dump the generated bytecode, in a debugging format, to the supplied
PrintStream
(if non-null
).Note: This method will not call a constructor on the generated instance, which may be more troublesome for spies than it is for mocks (as the real methods being called may rely on some state initialized by the constructor).
If you require a constructor be called, see
constructSpy(Class, PrintStream, Object...)
.- Type Parameters:
T
- The type being spied.- Parameters:
clz
- TheClass
to spy.trace
- If non-null, the resulting class will be dumped (with aTraceClassVisitor
) to the given stream.- Returns:
- A new spy instance.
- Since:
- 1.0
- See Also:
spy(Class)
,spy(MoxyEngine, Class, PrintStream)
,spy(Object)
,constructSpy(Class, PrintStream, Object...)
,mock(Class, PrintStream)
,MoxyStubber.thenCallRealMethod()
-
spy
public static <T> T spy(MoxyEngine engine, Class<T> clz, PrintStream trace)
Create a spy instance of the given class using the specified
MoxyEngine
.The spy will be created in the engine's default
ClassLoader
. For the default engine, this is the sameClassLoader
that loaded the Moxy framework.During creation of the mock, this method will also dump the generated bytecode, in a debugging format, to the supplied
PrintStream
(if non-null
).Note: This method will not call a constructor on the generated instance, which may be more troublesome for spies than it is for mocks (as the real methods being called may rely on some state initialized by the constructor).
If you require a constructor be called, see
constructSpy(MoxyEngine, Class, PrintStream, Object...)
.- Type Parameters:
T
- The type being spied.- Parameters:
engine
- TheMoxyEngine
implementation to use.clz
- TheClass
to spy.trace
- If non-null, the resulting class will be dumped (with aTraceClassVisitor
) to the given stream.- Returns:
- A new spy instance.
- Since:
- 1.0
- See Also:
spy(Class)
,spy(Class, PrintStream)
,spy(Object)
,constructSpy(MoxyEngine, Class, PrintStream, Object...)
,mock(MoxyEngine, Class, PrintStream)
,MoxyStubber.thenCallRealMethod()
-
constructSpy
public static <T> T constructSpy(Class<T> clz, Object... args)
Create a spy instance of the given class using the default
MoxyEngine
using a constructor call.The spy will be created in the engine's default
ClassLoader
. For the default engine, this is the sameClassLoader
that loaded the Moxy framework.This method uses a best-fit algorithm to determine which constructor to call based on the supplied arguments. Primitive types will be boxed/unboxed automatically.
- Type Parameters:
T
- The type being spied.- Parameters:
clz
- TheClass
to spy.args
- Constructor arguments for the spy.- Returns:
- A new spy instance.
- Since:
- 1.0
- See Also:
spy(Class)
,spy(Object)
,constructSpy(Class, PrintStream, Object...)
,constructSpy(MoxyEngine, Class, PrintStream, Object...)
,mock(Class)
,MoxyStubber.thenCallRealMethod()
-
constructSpy
public static <T> T constructSpy(Class<T> clz, PrintStream trace, Object... args)
Create a spy instance of the given class using the default
MoxyEngine
using a constructor call.The spy will be created in the engine's default
ClassLoader
. For the default engine, this is the sameClassLoader
that loaded the Moxy framework.During creation of the spy, this method will also dump the generated bytecode, in a debugging format, to the supplied
PrintStream
(if non-null
).This method uses a best-fit algorithm to determine which constructor to call based on the supplied arguments. Primitive types will be boxed/unboxed automatically.
- Type Parameters:
T
- The type being spied.- Parameters:
clz
- TheClass
to spy.trace
- If non-null, the resulting class will be dumped (with aTraceClassVisitor
) to the given stream.args
- Constructor arguments for the spy.- Returns:
- A new spy instance.
- Since:
- 1.0
- See Also:
spy(Class, PrintStream)
,spy(Object)
,constructSpy(Class, Object...)
,constructSpy(MoxyEngine, Class, PrintStream, Object...)
,mock(Class, PrintStream)
,MoxyStubber.thenCallRealMethod()
-
constructSpy
public static <T> T constructSpy(MoxyEngine engine, Class<T> clz, PrintStream trace, Object... args)
Create a spy instance of the given class using the supplied
MoxyEngine
using a constructor call.The spy will be created in the engine's default
ClassLoader
. For the default engine, this is the sameClassLoader
that loaded the Moxy framework.During creation of the spy, this method will also dump the generated bytecode, in a debugging format, to the supplied
PrintStream
(if non-null
).This method uses a best-fit algorithm to determine which constructor to call based on the supplied arguments. Primitive types will be boxed/unboxed automatically.
- Type Parameters:
T
- The type being spied.- Parameters:
engine
- TheMoxyEngine
implementation to use.clz
- TheClass
to spy.trace
- If non-null, the resulting class will be dumped (with aTraceClassVisitor
) to the given stream.args
- Constructor arguments for the spy.- Returns:
- A new spy instance.
- Since:
- 1.0
- See Also:
spy(MoxyEngine, Class, PrintStream)
,spy(Object)
,constructSpy(Class, Object...)
,constructSpy(Class, PrintStream, Object...)
,mock(MoxyEngine, Class, PrintStream)
,MoxyStubber.thenCallRealMethod()
-
isMock
public static boolean isMock(Class<?> clz)
Determines whether the supplied class is a mock class.
How this determination is made is engine-dependent, but may rely on the fact that the class has the (mandatory)
MoxyMock
annotation.- Parameters:
clz
- The class to query.- Returns:
true
if the class is a mock,false
otherwise.- Since:
- 1.0
-
isMock
public static boolean isMock(Object obj)
Determines whether the supplied object is a mock instance.
How this determination is made is engine-dependent, but may rely on the fact that the class has the (mandatory)
MoxyMock
annotation.- Parameters:
obj
- The object to query.- Returns:
true
if the object is a mock,false
otherwise.- Since:
- 1.0
-
resetMock
public static void resetMock(Object mock)
Reset the supplied mock, removing all stubbing that was previously applied.
Note that this does not reset previous invocation data for the mock. If you wish to reset that, see
MoxyEngine.reset()
.- Parameters:
mock
- The mock to reset.- Since:
- 1.0
- See Also:
MoxyEngine.reset()
,resetMock(MoxyEngine, Object)
,MoxyEngine.resetMock(Object)
-
resetMock
public static void resetMock(MoxyEngine engine, Object mock)
Reset the supplied mock using the supplied engine, removing all stubbing that was previously applied.
Note that this does not not reset previous invocation data for the mock. If you wish to reset that, see
MoxyEngine.reset()
.- Parameters:
engine
- TheMoxyEngine
to use.mock
- The mock to reset.- Since:
- 1.0
- See Also:
MoxyEngine.reset()
,MoxyEngine.resetMock(Object)
-
isMock
public static boolean isMock(MoxyEngine engine, Class<?> clz)
Determines whether the supplied class is a mock class in the context of the supplied
MoxyEngine
.How this determination is made is engine-dependent.
- Parameters:
engine
- TheMoxyEngine
implementation to use.clz
- The class to query.- Returns:
true
if the class is a mock,false
otherwise.- Since:
- 1.0
-
isMock
public static boolean isMock(MoxyEngine engine, Object obj)
Determines whether the supplied class is a mock class in the context of the supplied
MoxyEngine
.How this determination is made is engine-dependent.
- Parameters:
engine
- TheMoxyEngine
implementation to use.obj
- The object to query.- Returns:
true
if the object is a mock,false
otherwise.- Since:
- 1.0
-
when
public static <T> MoxyStubber<T> when(InvocationSupplier<T> invocation)
Starts stubbing of the mock invocation in the supplied lambda expression using the currently-set (or default)
MoxyEngine
.The invocation of the mock is used to determine which method and argument combination is to be stubbed and is not counted toward mock invocation, return or throw counters.
Example usage:
Moxy.when(() -> mock.someMethod("one", "two")).thenReturn("three");
The arguments passed to the mock within the lambda may be either immediate arguments, other mocks, or argument matchers (see
Matchers
).This method begins a new chain of stubbing for the given invocation - Any prior stubbing applied is discarded, to be replaced by the stubbing applied on the returned
MoxyStubber
.See
MoxyStubber
for details on the stubbing methods available.- Type Parameters:
T
- The type being stubbed (return type of the mock method).- Parameters:
invocation
- A lambda that will invoke the method to be stubbed.- Returns:
- A
MoxyStubber
that will stub the given method. - Since:
- 1.0
- See Also:
when(InvocationRunnable)
,when(MoxyEngine, InvocationSupplier)
,MoxyStubber
-
when
public static MoxyVoidStubber when(InvocationRunnable invocation)
Starts stubbing of the mock invocation in the supplied lambda expression using the currently-set (or default)
MoxyEngine
.The Java compiler will automatically select this overload of the
when
method when the method to be stubbed is void.The invocation of the mock is used to determine which method and argument combination is to be stubbed and is not counted toward mock invocation, return or throw counters.
Example usage:
Moxy.when(() -> mock.voidMethod("one", "two")).thenThrow("three");
The arguments passed to the mock within the lambda may be either immediate arguments, other mocks, or argument matchers (see
Matchers
).This method begins a new chain of stubbing for the given invocation - Any prior stubbing applied is discarded, to be replaced by the stubbing applied on the returned
MoxyVoidStubber
.See
MoxyStubber
for details on the stubbing methods available.- Parameters:
invocation
- A lambda that will invoke the method to be stubbed.- Returns:
- A
MoxyStubber
that will stub the given method. - Since:
- 1.0
- See Also:
when(InvocationSupplier)
,when(MoxyEngine, InvocationRunnable)
,MoxyStubber
-
when
public static <T> MoxyStubber<T> when(MoxyEngine engine, InvocationSupplier<T> invocation)
Starts stubbing of the mock invocation in the supplied lambda expression using the specified
MoxyEngine
.The invocation of the mock is used to determine which method and argument combination is to be stubbed and is not counted toward mock invocation, return or throw counters.
Example usage:
Moxy.when(engine, () -> mock.someMethod("one", "two")).thenReturn("three");
The arguments passed to the mock within the lambda may be either immediate arguments, other mocks, or argument matchers (see
Matchers
).See
MoxyStubber
for details on the stubbing methods available.- Type Parameters:
T
- The type being stubbed (return type of the mocked method).- Parameters:
engine
- TheMoxyEngine
implementation to use.invocation
- A lambda that will invoke the method to be stubbed.- Returns:
- A
MoxyStubber
that will stub the given method. - Since:
- 1.0
- See Also:
when(MoxyEngine, InvocationRunnable)
,when(InvocationSupplier)
,MoxyStubber
-
when
public static MoxyVoidStubber when(MoxyEngine engine, InvocationRunnable invocation)
Starts stubbing of the mock invocation in the supplied lambda expression using the specified
MoxyEngine
.The Java compiler will automatically select this overload of the
when
method when the method to be stubbed is void.The invocation of the mock is used to determine which method and argument combination is to be stubbed and is not counted toward mock invocation, return or throw counters.
Example usage:
Moxy.when(() -> mock.voidMethod(engine, "one", "two")).thenThrow("three");
The arguments passed to the mock within the lambda may be either immediate arguments, other mocks, or argument matchers (see
Matchers
).See
MoxyStubber
for details on the stubbing methods available.- Parameters:
engine
- TheMoxyEngine
implementation to use.invocation
- A lambda that will invoke the method to be stubbed.- Returns:
- A
MoxyStubber
that will stub the given method. - Since:
- 1.0
- See Also:
when(InvocationSupplier)
,when(MoxyEngine, InvocationRunnable)
,MoxyStubber
-
assertMock
public static MoxyVerifier assertMock(InvocationRunnable invocation)
Starts verification of the mock invocation in the supplied lambda expression using the currently-set (or default)
MoxyEngine
.The invocation of the mock is used to determine which method and argument combination is to be verified and is not counted toward mock invocation, return or throw counters.
Example usage:
Moxy.assertMock(() -> mock.voidMethod(engine, "one", "two")).wasCalled();
The arguments passed to the mock within the lambda may be either immediate arguments, other mocks, or argument matchers (see
Matchers
).See
MoxyStubber
for details on the stubbing methods available.- Parameters:
invocation
- A lambda that will invoke the method to be stubbed.- Returns:
- A
MoxyVerifier
that will verify the given method. - Since:
- 1.0
- See Also:
assertMock(MoxyEngine, InvocationRunnable)
,MoxyVerifier
-
assertMock
public static MoxyVerifier assertMock(MoxyEngine engine, InvocationRunnable invocation)
Starts verification of the mock invocation in the supplied lambda expression using the specified
MoxyEngine
.The invocation of the mock is used to determine which method and argument combination is to be verified and is not counted toward mock invocation, return or throw counters.
Example usage:
Moxy.assertMock(() -> mock.voidMethod(engine, "one", "two")).wasCalled();
The arguments passed to the mock within the lambda may be either immediate arguments, other mocks, or argument matchers (see
Matchers
).See
MoxyStubber
for details on the stubbing methods available.- Parameters:
engine
- TheMoxyEngine
implementation to use.invocation
- A lambda that will invoke the method to be stubbed.- Returns:
- A
MoxyVerifier
that will verify the given method. - Since:
- 1.0
- See Also:
assertMock(InvocationRunnable)
,MoxyVerifier
-
assertMocks
public static MoxyMultiVerifier assertMocks(InvocationRunnable invocation)
Starts verification of one or more mock invocations at the same time. This allows multiple invocations to be checked together to ensure ordering, for example.
This method uses the current default
MoxyEngine
.The invocation of the mock is used to determine which method and argument combination is to be verified and is not counted toward mock invocation, return or throw counters.
Example usage:
engine.assertMocks(() -> { mock.voidMethod(engine, "one", "two")).wasCalled(); mock.anotherMethod(); anotherMock.someMethod("five"); mock.finalMethod("Bees"); }) .wereAllCalledOnce() .inThatOrder();
The arguments passed to the mocks within the lambda may be either immediate arguments, other mocks, or argument matchers (see
Matchers
).See
MoxyMultiVerifier
for details on the verifying methods available.- Parameters:
invocation
- A lambda that will invoke the methods to be verified.- Returns:
- A
MoxyMultiVerifier
that will verify the invocations. - Since:
- 1.0
- See Also:
assertMock(InvocationRunnable)
-
assertMocks
public static MoxyMultiVerifier assertMocks(MoxyEngine engine, InvocationRunnable invocation)
Starts verification of one or more mock invocations at the same time. This allows multiple invocations to be checked together to ensure ordering, for example.
This method uses the supplied
MoxyEngine
.The invocation of the mock is used to determine which method and argument combination is to be verified and is not counted toward mock invocation, return or throw counters.
Example usage:
engine.assertMocks(() -> { mock.voidMethod(engine, "one", "two")).wasCalled(); mock.anotherMethod(); anotherMock.someMethod("five"); mock.finalMethod("Bees"); }) .wereAllCalledOnce() .inThatOrder();
The arguments passed to the mocks within the lambda may be either immediate arguments, other mocks, or argument matchers (see
Matchers
).See
MoxyMultiVerifier
for details on the verifying methods available.- Parameters:
engine
- TheMoxyEngine
to use.invocation
- A lambda that will invoke the methods to be verified.- Returns:
- A
MoxyMultiVerifier
that will verify the invocations. - Since:
- 1.0
- See Also:
assertMock(InvocationRunnable)
-
-