Package org.jmock
Class Mockery
- java.lang.Object
-
- org.jmock.Mockery
-
- All Implemented Interfaces:
org.hamcrest.SelfDescribing
- Direct Known Subclasses:
JUnit3Mockery
,JUnit4Mockery
,JUnit5Mockery
public class Mockery extends java.lang.Object implements org.hamcrest.SelfDescribing
A Mockery represents the context, or neighbourhood, of the object(s) under test. The neighbouring objects in that context are mocked out. The test specifies the expected interactions between the object(s) under test and its neighbours and the Mockery checks those expectations while the test is running.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
Mockery.MockObject
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<Invocation>
actualInvocations
private ReturnDefaultValueAction
defaultAction
private InvocationDispatcher
dispatcher
private ExpectationErrorTranslator
expectationErrorTranslator
private java.lang.Error
firstError
private Imposteriser
imposteriser
private java.util.Set<java.lang.String>
mockNames
private MockObjectNamingScheme
namingScheme
private ThreadingPolicy
threadingPolicy
-
Constructor Summary
Constructors Constructor Description Mockery()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addExpectation(Expectation expectation)
Adds an expected invocation that the object under test will perform upon objects in its context during the test.void
assertIsSatisfied()
Fails the test if there are any expectations that have not been met.void
checking(ExpectationBuilder expectations)
Specifies the expected invocations that the object under test will perform upon objects in its context during the test.private void
describeHistory(org.hamcrest.Description description)
private void
describeMismatch(Invocation invocation, org.hamcrest.Description description)
void
describeTo(org.hamcrest.Description description)
private java.lang.Object
dispatch(Invocation invocation)
private ExpectationError
mismatchDescribing(ExpectationError e)
<T> T
mock(java.lang.Class<T> typeToMock)
Creates a mock object of type typeToMock and generates a name for it.<T> T
mock(java.lang.Class<T> typeToMock, java.lang.String name)
Creates a mock object of type typeToMock with the given name.Sequence
sequence(java.lang.String name)
Returns a new sequence that is used to constrain the order in which expectations can occur.void
setDefaultResultForType(java.lang.Class<?> type, java.lang.Object result)
Sets the result returned for the given type when no return value has been explicitly specified in the expectation.void
setExpectationErrorTranslator(ExpectationErrorTranslator expectationErrorTranslator)
Changes the expectation error translator used to translate expectation errors into errors that report test failures.void
setImposteriser(Imposteriser imposteriser)
Changes the imposteriser used to adapt mock objects to the mocked type.void
setNamingScheme(MockObjectNamingScheme namingScheme)
Changes the naming scheme used to generate names for mock objects that have not been explicitly named in the test.void
setThreadingPolicy(ThreadingPolicy threadingPolicy)
Changes the policy by which the Mockery copes with multiple threads.States
states(java.lang.String name)
Returns a new state machine that is used to constrain the order in which expectations can occur.
-
-
-
Field Detail
-
imposteriser
private Imposteriser imposteriser
-
expectationErrorTranslator
private ExpectationErrorTranslator expectationErrorTranslator
-
namingScheme
private MockObjectNamingScheme namingScheme
-
threadingPolicy
private ThreadingPolicy threadingPolicy
-
mockNames
private final java.util.Set<java.lang.String> mockNames
-
defaultAction
private final ReturnDefaultValueAction defaultAction
-
actualInvocations
private final java.util.List<Invocation> actualInvocations
-
dispatcher
private InvocationDispatcher dispatcher
-
firstError
private java.lang.Error firstError
-
-
Method Detail
-
setDefaultResultForType
public void setDefaultResultForType(java.lang.Class<?> type, java.lang.Object result)
Sets the result returned for the given type when no return value has been explicitly specified in the expectation.- Parameters:
type
- The type for which to return result.result
- The value to return when a method of return type type is invoked for which an explicit return value has has not been specified.
-
setImposteriser
public void setImposteriser(Imposteriser imposteriser)
Changes the imposteriser used to adapt mock objects to the mocked type. The default imposteriser allows a test to mock interfaces but not classes, so you'll have to plug a different imposteriser into the Mockery if you want to mock classes.- Parameters:
imposteriser
- makes mocks
-
setNamingScheme
public void setNamingScheme(MockObjectNamingScheme namingScheme)
Changes the naming scheme used to generate names for mock objects that have not been explicitly named in the test. The default naming scheme names mock objects by lower-casing the first letter of the class name, so a mock object of type BananaSplit will be called "bananaSplit" if it is not explicitly named in the test.- Parameters:
namingScheme
- names mocks for failure reports
-
setExpectationErrorTranslator
public void setExpectationErrorTranslator(ExpectationErrorTranslator expectationErrorTranslator)
Changes the expectation error translator used to translate expectation errors into errors that report test failures. By default, expectation errors are not translated and are thrown as errors of typeExpectationError
. Plug in a new expectation error translator if you want your favourite test framework to report expectation failures using its own error type.- Parameters:
expectationErrorTranslator
- translator for your test framework
-
setThreadingPolicy
public void setThreadingPolicy(ThreadingPolicy threadingPolicy)
Changes the policy by which the Mockery copes with multiple threads. The default policy throws an exception if the Mockery is called from different threads.- Parameters:
threadingPolicy
- how to handle different threads.- See Also:
Synchroniser
-
mock
public <T> T mock(java.lang.Class<T> typeToMock)
Creates a mock object of type typeToMock and generates a name for it.- Type Parameters:
T
- is the class of the mock- Parameters:
typeToMock
- is the class of the mock- Returns:
- the mock of typeToMock
-
mock
public <T> T mock(java.lang.Class<T> typeToMock, java.lang.String name)
Creates a mock object of type typeToMock with the given name.- Type Parameters:
T
- is the class of the mock- Parameters:
typeToMock
- is the class of the mockname
- is the name of the mock object that will appear in failures- Returns:
- the mock of typeToMock
-
sequence
public Sequence sequence(java.lang.String name)
Returns a new sequence that is used to constrain the order in which expectations can occur.- Parameters:
name
- The name of the sequence.- Returns:
- A new sequence with the given name.
-
states
public States states(java.lang.String name)
Returns a new state machine that is used to constrain the order in which expectations can occur.- Parameters:
name
- The name of the state machine.- Returns:
- A new state machine with the given name.
-
checking
public void checking(ExpectationBuilder expectations)
Specifies the expected invocations that the object under test will perform upon objects in its context during the test. The builder is responsible for interpreting high-level, readable API calls to construct an expectation. This method can be called multiple times per test and the expectations defined in each block are combined as if they were defined in same order within a single block.- Parameters:
expectations
- that will be checked
-
addExpectation
public void addExpectation(Expectation expectation)
Adds an expected invocation that the object under test will perform upon objects in its context during the test. This method allows a test to define an expectation explicitly, bypassing the high-level API, if desired.- Parameters:
expectation
- to check
-
assertIsSatisfied
public void assertIsSatisfied()
Fails the test if there are any expectations that have not been met.
-
describeTo
public void describeTo(org.hamcrest.Description description)
- Specified by:
describeTo
in interfaceorg.hamcrest.SelfDescribing
-
describeMismatch
private void describeMismatch(Invocation invocation, org.hamcrest.Description description)
-
describeHistory
private void describeHistory(org.hamcrest.Description description)
-
dispatch
private java.lang.Object dispatch(Invocation invocation) throws java.lang.Throwable
- Throws:
java.lang.Throwable
-
mismatchDescribing
private ExpectationError mismatchDescribing(ExpectationError e)
-
-