public class MockBehaviorRecorder extends BeanRecorder
ObjectRecorder that records the behavior of an object tree as a Java class file.
The resulting factory class uses EasyMock mock objects to mock the expected behavior of methods
called on objects throughout the object tree.
Recording example as a JUnit test:
public void testRecordJavadocExample()
{
MockBehaviorRecorder recorder = new MockBehaviorRecorder();
recorder.setDestination( "recordings" );
Person person = new Person();
Person dad = new Person();
dad.setName( "dad" );
person.setDad( dad );
person = recorder.record( person );
person.setName( "Joe" );
assertEquals( "Joe", person.getName() );
assertEquals( "dad", person.getDad().getName() );
assertEquals( "dad", person.getDad().getName() );
Person mom = new Person();
mom.setName( "mom" );
person.setMom( mom );
recorder.endRecording();
}
Note that the "person" instance is swapped with an instrumented instance of the "person" object on the line
person = recorder.record( person );
The above example will record the behavior of the "person" instance as a tree of mock objects in the Java class:
public class PersonFactory extends MockFactory
{
protected Person person;
public Person buildPerson()
{
if ( person != null )
{
return person;
}
person = createStrictMock( Person.class );
person.setName( "Joe" );
expect( person.getName() ).andReturn( "Joe" );
expect( person.getDad() ).andReturn( buildPerson_3_1() );
expect( person.getDad() ).andReturn( buildPerson_3_1() );
person.setMom( buildPerson_5_1() );
replay( person );
return person;
}
protected Person person_3_1;
protected Person buildPerson_3_1()
{
if ( person_3_1 != null )
{
return person_3_1;
}
person_3_1 = createStrictMock( Person.class );
expect( person_3_1.getName() ).andReturn( "dad" );
expect( person_3_1.getName() ).andReturn( "dad" );
replay( person_3_1 );
return person_3_1;
}
protected Person person_5_1;
protected Person buildPerson_5_1()
{
if ( person_5_1 != null )
{
return person_5_1;
}
person_5_1 = createStrictMock( Person.class );
replay( person_5_1 );
return person_5_1;
}
}
To reconstruct the instance of "person" in a unit test and assert that all method calls are identical to the recorded behavior:
public void testPlaybackJavadocExample()
{
Person person = new PersonFactory().buildPerson();
person.setName( "Joe" );
assertEquals( "Joe", person.getName() );
assertEquals( "dad", person.getDad().getName() );
assertEquals( "dad", person.getDad().getName() );
Person mom = new Person();
mom.setName( "mom" );
person.setMom( mom );
}
Note that the above unit test will fail because the mock objects are strictly performing checks on all objects they are interacting with in the object tree. In the playback test case, the instance of "mom" is not the same instance as the recorded test case, so the mock objects will complain. In order to make this test useful, it is necessary to edit the recorded PersonFactory class and change the line
person.setMom( buildPerson_5_1() );
to
person.setMom( anyObject( Person.class ) );
This may be a bug. If you disagree with this behavior, please open an issue on the Java Test Object Recorder site.
The MockBehaviorRecorder is "instance aware" and supports Collection and Map objects. This class
requires the EasyMock 3.0 jar and the cglib 2.2 jar.
| Modifier and Type | Field and Description |
|---|---|
protected boolean |
fieldNice |
protected java.util.Set<java.lang.Class> |
fieldPreferredClasses |
protected java.util.Set<java.lang.Class> |
fieldPreferredInterfaces |
fieldClassesForInterfaces, fieldFactoryBuilder, fieldInstanceTree, fieldStopClassesfieldDestinationDirectory, fieldJavaFileWriterCLASS, fieldDescriptor, fieldObject, fieldPrintWriter, fieldStaticClasses, fieldSuperClass, IMPORT, PACKAGE, PRIVATE, PROTECTED, PUBLIC, SPACE, tabDepth| Constructor and Description |
|---|
MockBehaviorRecorder() |
| Modifier and Type | Method and Description |
|---|---|
void |
addPreferredInterface(java.lang.Class inClass) |
protected boolean |
canInstrument(java.lang.Object result) |
protected boolean |
canInstrumentArray(java.lang.Object result) |
protected boolean |
canInstrumentClass(java.lang.Object result) |
protected java.util.Set<java.lang.Class> |
classes() |
protected BehaviorInstanceTree |
createBehaviorInstanceTree(java.lang.Object object) |
protected InstanceTree |
createInstanceTree(java.lang.Object object) |
protected MethodBuilderFactory |
createMethodBuilderFactory() |
protected MockMethodBuilderFactory |
createMockMethodBuilderFactory() |
protected ProxyFactory |
createProxyFactory(java.lang.Class inClass) |
protected void |
debug(java.lang.Object inObject) |
java.lang.Object |
endRecording()
End monitoring of the object being recorded and trigger the writing of the factory file.
|
java.util.Set<java.lang.Class> |
getPreferredClasses() |
java.util.Set<java.lang.Class> |
getPreferredInterfaces() |
protected boolean |
hasPreferedInterface(java.lang.Class inClass) |
protected void |
initializeInstanceTree(java.lang.Object inObject) |
protected <T> T |
instrument(T inObject) |
protected <T> T[] |
instrumentedArray(T[] inArray) |
protected boolean |
isEnhanced(java.lang.Class objectClass) |
protected boolean |
isFinal(java.lang.Object result) |
boolean |
isNice() |
protected boolean |
isPrimitive(java.lang.Object result) |
protected boolean |
isPublic(java.lang.Object result) |
protected BehaviorInstanceTree |
newInstanceTree(java.lang.Object inObject) |
protected java.lang.Class |
preferredInterface(java.lang.Class inClass) |
<T> T |
record(T inObject) |
void |
setNice(boolean nice)
Create "nice" EasyMock objects rather than the default strict mocks.
|
addBuilderFactory, closeFile, createMethodBuilder, getClassesForInterfaces, getFactoryBuilder, getInstanceTree, getObject, getPrintWriter, getStopClasses, setObject, stopDescent, useTypeForInterface, writeObjectBuilderMethodclose, createDefaultDescriptor, createFileWriter, getDestination, getJavaFileWriter, javaFile, objectClass, packagePath, parentDirectory, setDestination, setDestinationaddStaticImport, closeBrace, extendClass, getClassName, getDescriptor, getPackageName, getStaticClasses, getSuperClass, newLine, openBrace, packageName, setClassName, setDescriptor, setSuperClass, setWriter, tab, tabs, write, writeClassDeclaration, writeImports, writeLine, writeObject, writePackage, writeStaticImportsprotected java.util.Set<java.lang.Class> fieldPreferredInterfaces
protected java.util.Set<java.lang.Class> fieldPreferredClasses
protected boolean fieldNice
public java.util.Set<java.lang.Class> getPreferredInterfaces()
public java.util.Set<java.lang.Class> getPreferredClasses()
public void addPreferredInterface(java.lang.Class inClass)
protected java.util.Set<java.lang.Class> classes()
classes in class BeanRecorderpublic <T> T record(T inObject)
record in interface ObjectRecorderrecord in class BeanRecorderprotected void debug(java.lang.Object inObject)
protected <T> T instrument(T inObject)
protected ProxyFactory createProxyFactory(java.lang.Class inClass)
protected boolean hasPreferedInterface(java.lang.Class inClass)
protected java.lang.Class preferredInterface(java.lang.Class inClass)
protected boolean isEnhanced(java.lang.Class objectClass)
protected boolean canInstrument(java.lang.Object result)
protected boolean canInstrumentClass(java.lang.Object result)
protected boolean isFinal(java.lang.Object result)
protected boolean isPrimitive(java.lang.Object result)
protected boolean isPublic(java.lang.Object result)
protected boolean canInstrumentArray(java.lang.Object result)
protected <T> T[] instrumentedArray(T[] inArray)
protected BehaviorInstanceTree newInstanceTree(java.lang.Object inObject)
protected void initializeInstanceTree(java.lang.Object inObject)
public java.lang.Object endRecording()
protected MethodBuilderFactory createMethodBuilderFactory()
createMethodBuilderFactory in class BeanRecorderprotected MockMethodBuilderFactory createMockMethodBuilderFactory()
protected InstanceTree createInstanceTree(java.lang.Object object)
createInstanceTree in class BeanRecorderprotected BehaviorInstanceTree createBehaviorInstanceTree(java.lang.Object object)
public boolean isNice()
public void setNice(boolean nice)
nice -