|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--net.sf.surrogate.core.SurrogateManager
The link between Mock/ Unit Test objects and AspectJ code.
The SurrogateManager is the common controller for Junit tests manipulating MockObjects and mock methods.
getInstance()
reset()
addMock(Object)
addMockMethod(MockMethod)
removeMock(Object)
removeMockMethod(MockMethod)
getInstance()
getMockExecutor(JoinPoint)
reset()
method before
starting to use the SurrogateManager to avoid independence from other
testcases which might have been run earlier from within the same VM.
System.out
. Each debug line is on
the following format:
surrogate:<joinpoint id>|added|removed=<mock id> Example: surrogate:added=public static native long java.lang.System.currentTimeMillis() surrogate:added=net.sf.surrogate.example.MockFileWriter@38e059 surrogate:call(long java.lang.System.currentTimeMillis())=public static native long java.lang.System.currentTimeMillis() surrogate:call(java.io.FileWriter(String))=net.sf.surrogate.example.MockFileWriter@38e059 surrogate:call(java.io.BufferedWriter(Writer))=nullI.e. the unit test has added mocks for
currentTimeMillis
and
the FileWriter
. As expected, mocks were returned for the
currentTimeMillis
and FileWriter
mockJoinPoint's but no mock was found for the BufferedWriter
joinpoint (null
was returned).
SurrogateCalls
,
MockMethod
Inner Class Summary | |
static interface |
SurrogateManager.MockExecutor
Defines, towards the AspectJ layer, the mock object or method to be executed. |
Method Summary | |
java.lang.Object |
addMock(java.lang.Object o)
Adds a Mock object to the manager for later lookup by Aspect code. |
MockMethod |
addMockMethod(MockMethod m)
Adds a mock method to the manager for later lookup by Aspect code. |
protected static SurrogateManager |
createInstance()
Created an instance of the manager. |
static SurrogateManager |
getInstance()
Gets the manager singleton |
(package private) MockMethod |
getMockConstructor(java.lang.reflect.Constructor c)
Gets a registered Mock method reference for a constructor |
SurrogateManager.MockExecutor |
getMockExecutor(org.aspectj.lang.JoinPoint p)
Gets the mock object or mock method for a joinpoint. |
protected MockMethod |
getMockMethod(org.aspectj.lang.JoinPoint p)
Checks if an AspectJ "mock" joinpoint has an assoicated mock method registered by the manager. |
(package private) MockMethod |
getMockMethod(java.lang.reflect.Method m)
Gets a registered Mock method reference for a method. |
(package private) java.lang.Object |
getMockObject(java.lang.Class myClassOrInterface)
Locates and gets a Mock object for an interface or a class. |
protected java.lang.Object |
getReturnedClassMock(org.aspectj.lang.JoinPoint p)
Checks if an AspectJ "mock" joinpoint has an assoicated mock object registered by the manager. |
java.lang.Object |
removeMock(java.lang.Object o)
Removes a mock from the manager. |
MockMethod |
removeMockMethod(MockMethod m)
Removes a mock method from the manager. |
void |
reset()
Removes all Mock objects and methods from the list of active objects. |
Methods inherited from class java.lang.Object |
|
Method Detail |
public static SurrogateManager getInstance()
reset()
protected static SurrogateManager createInstance()
public void reset()
public java.lang.Object addMock(java.lang.Object o)
Note that for objects to be substituted with their mock implementation,
there must have been defined a pointcut
intercepting the
method call or method execution. Otherwise, the mock object will never be
substituted, even if it has been registered. See SurrogateCalls
for pointcut definition details.
Surrogate uses the java.lang.Class.isAssignableFrom
to see
whether the registered mock can subsitute a "real" object. It does this
by looking up the declared return type signature of the method or the
class signature of the constructor call.
Example usage:
SurrogateManager mm = SurrogateManager.getInstance(); mm.reset(); MockCustomerService mock = new MockCustomerService(); mm.addMock(mock); mock.setGetCustomerReturnValue("MockCustomer"); ...
o
- the Mock object to add. Must be non-null.addMockMethod(MockMethod)
,
removeMock(Object)
,
SurrogateCalls
public MockMethod addMockMethod(MockMethod m)
Note that for objects to be substituted with their mock implementation,
there must have been defined a pointcut
intercepting the
method call or method execution. Otherwise, the mock object will never be
substituted, even if it has been registered. See SurrogateCalls
for pointcut definition details.
Example usage:
SurrogateManager mm = SurrogateManager.getInstance(); mm.reset(); MockMethod mockTime = mm.addMockMethod(new MockMethod(System.class,"currentTimeMillis")); mockTime.addReturnValue(1000L); mockTime.addReturnValue(2000L); mockTime.setExpectedCalls(2); ... Call object using System.currentTimeMillis mockTime.verify(); ...
m
- the Mock object to add. Must be non-null.addMock(Object)
,
removeMockMethod(MockMethod)
public java.lang.Object removeMock(java.lang.Object o)
o
- the object to remove.null
if the object was not
foundaddMock(Object)
public MockMethod removeMockMethod(MockMethod m)
o
- the object to remove.null
if the mock method was
not found.addMockMethod(MockMethod)
java.lang.Object getMockObject(java.lang.Class myClassOrInterface)
myClassOrInterface
by searching in the
list of Mock objects for the first object which
myClassOrInterface
is assignable frommyClassOrInterface
- the interface/class to find a mock object fornull
if
no such mock object has been registered via the
addMock
method.addMock(Object)
,
Class.isAssignableFrom(java.lang.Class)
MockMethod getMockMethod(java.lang.reflect.Method m)
Mock method reference
for a method. This
method should normally only be called from the aspect code.m
- the Method to checknull
if no
such reference exists for the methodaddMockMethod(MockMethod)
MockMethod getMockConstructor(java.lang.reflect.Constructor c)
Mock method reference
for a constructorc
- the constructor to checknull
if no
such reference exists for the methodaddMockMethod(MockMethod)
protected java.lang.Object getReturnedClassMock(org.aspectj.lang.JoinPoint p) throws java.lang.IllegalArgumentException
p
- the join pointnull
if no match
is foundprotected MockMethod getMockMethod(org.aspectj.lang.JoinPoint p) throws java.lang.IllegalArgumentException, java.lang.NoSuchMethodException
p
- the join pointnull
if no match
is foundjava.lang.IllegalArgumentException
- if the joinpoint signature is not a "method" or "constructor"
signaturepublic SurrogateManager.MockExecutor getMockExecutor(org.aspectj.lang.JoinPoint p) throws java.lang.IllegalArgumentException, java.lang.NoSuchMethodException
SurrogateCalls
advice but can also be called from other
advices which might want to check if a mock matches the joinpoint.
The rules are as follows
MockMethod
matches the joinpoint, this method is
returned.
MockExecutor
null
is returned.
MockExecutor
return value, call the MockExecutor.execute
with the
same JoinPoint as used as arguments to
getMockExecutor
. If the returned
MockExecutor
is null
, the advice should
return a sensible value, e.g. with return proceed();
p
- the thisJoinPoint
on the advice executingnull
if no
match has been found with a registered mock object or mock methodjava.lang.IllegalArgumentException
- if the joinpoint signature is not a "constructor" or "method"
signature.java.lang.NoSuchMethodException
- if the joinpoint is inconsistent. This should normally be
considered as a bug.SurrogateCalls
,
addMock(Object)
,
addMockMethod(MockMethod)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |