public class MockEditingContext
extends com.webobjects.eocontrol.EOEditingContext
MockEditingContext is a subclass of EOEditingContext
that can be used for fast in-memory testing of business objects.
Unit tests for logic which use fetch specifications or save to an editing context can be relative slow because full roundtrips to a database are being performed. Testing the same logic with a MockEditingContext ensures that nothing is being saved to or read from a database resulting in shorter execution time of the unit test. Also, you don't risk invalidating the persistent data with broken test cases.
Assuming there is an EOCustomObject called Person with the
attributes name, age, nationality and
partner. nationality is of entity Country
for which there is a constant list of objects in the database. We want to write a test for the
static Person method createFromDescription(String, EOEditingContext).
import org.junit.*;
import static org.junit.Assert.*;
import org.wounittest.*;
import static org.wounittest.WOAssert.*;
public class PersonTest extends EOFTest {
Person jane;
@Before
public void setUp() throws Exception {
super.setUp();
mockEditingContext().setEntityNamesToGetFromDatabase(new NSArray("Country"));
jane = (Person)mockEditingContext().createSavedObject("Person");
jane.setName("Jane Doe");
jane.setNationalityRelationship(Country.withName("Canada", mockEditingContext()));
}
@Test
public void creationFromDescriptionWithExistingPartner() {
Person newPerson = Person.createFromDescription("John Doe, 34, Canada, Jane Doe",
mockEditingContext());
assertEquals("John Doe", newPerson.name());
assertEquals(34, newPerson.age());
assertSame(Country.withName("Canada", mockEditingContext()), newPerson.nationality());
assertSame(jane, newPerson.partner());
}
@Test
public void creationFromDescriptionWithUnknownPartner() {
Person newPerson = Person.createFromDescription("Bla Fasel, 12, Germany, Jeniffer Doe",
mockEditingContext());
assertEquals("Blah Fasel", newPerson.name());
assertEquals(12, newPerson.age());
assertSame(Country.withName("Germany", mockEditingContext()), newPerson.nationality());
assertNull(newPerson.partner());
}
@Test(expected = UnknownCountryException.class)
public void creationFromDescriptionWithUnknownCountry() {
Person.createFromDescription("Bla Fasel, 12, Wawaland, Jane Doe",
mockEditingContext());
}
}
Additional information can be found in WOUTMockEditingContextTest.java.
com.webobjects.eocontrol.EOEditingContext._EventLoggingEnabler, com.webobjects.eocontrol.EOEditingContext.Delegate, com.webobjects.eocontrol.EOEditingContext.EditingContextEvent, com.webobjects.eocontrol.EOEditingContext.Editor, com.webobjects.eocontrol.EOEditingContext.MessageHandlercom.webobjects.eocontrol.EOKeyValueArchiving._NullValueSupport, com.webobjects.eocontrol.EOKeyValueArchiving._NumberSupport, com.webobjects.eocontrol.EOKeyValueArchiving._TimestampSupport, com.webobjects.eocontrol.EOKeyValueArchiving.Awaking, com.webobjects.eocontrol.EOKeyValueArchiving.FinishInitialization, com.webobjects.eocontrol.EOKeyValueArchiving.Support| Modifier and Type | Field and Description |
|---|---|
protected NSArray |
entityNamesToGetFromDatabase |
protected static int |
fakePrimaryKeyCounter |
protected NSMutableArray |
ignoredObjects |
_CLASS, _EditingContextDidChangeSharedEditingContextNotification, _EditingContextEditorHasChangesNotification, _IsEventLoggingEnabled, EditingContextDidSaveChangesNotification, EditingContextFlushChangesRunLoopOrdering, ObjectsChangedInEditingContextNotification| Constructor and Description |
|---|
MockEditingContext()
Constructs a MockEditingContext.
|
| Modifier and Type | Method and Description |
|---|---|
protected com.webobjects.eocontrol.EOGlobalID |
assignFakeGlobalIDToObject(EOCustomObject anObject)
Internal helper method for
insertSavedObject. |
EOCustomObject |
createSavedObject(String anEntityName)
Convenience cover method for
insertSavedObject. |
void |
dispose()
Extends the implementation inherited from EOEditingContext to delete ignoredObjects.
|
void |
insertSavedObject(EOCustomObject anObject)
Inserts a Custom Object into the receiver and makes it look as if it was fetched from the database.
|
void |
objectWillChange(Object anObject)
Overrides the implementation inherited from EOEditingContext to ignore objects registered with
insertSavedObject. |
com.webobjects.eocontrol.EOObjectStore |
rootObjectStore()
Overwritten to return the
defaultParentObjectStore. |
void |
setEntityNamesToGetFromDatabase(NSArray theEntityNamesToGetFromDatabase)
Defines which entities should be fetched from the rootObjectStore.
|
_clearChangedThisTransaction, _clearOriginalSnapshotAndInitializeRec, _clearOriginalSnapshotForObject, _defaultEditingContextNowInitialized, _defaultSharedEditingContextWasInitialized, _editorHasChanges, _EOAssertSafeMultiThreadedAccess, _EOAssertSafeMultiThreadedAccess, _EOAssertSafeMultiThreadedReadAccess, _globalIDChanged, _globalIDsForObjects, _initWithParentObjectStore, _insertObjectWithGlobalID, _invalidatedAllObjectsInStore, _invalidateObjectsDuringSave, _noop, _objectBasedChangeInfoForGIDInfo, _objectsChangedInStore, _objectsInitializedInSharedContext, _processEndOfEventNotification, _processGlobalIDChanges, _processInitializedObjectsInSharedContext, _processNotificationQueue, _processObjectStoreChanges, _processRecentChanges, _resetAllChanges, _resetAllChanges, _retainCountForObjectWithGlobalID, _undoDelete, _undoManagerCheckpoint, _undoUpdate, addEditor, arrayFaultWithSourceGlobalID, committedSnapshotForObject, currentEventSnapshotForObject, decodeWithKeyValueUnarchiver, defaultFetchTimestampLag, defaultParentObjectStore, delegate, deletedObjects, deleteObject, editingContextDidForgetObjectWithGlobalID, editors, editorsHaveChanges, encodeObjectWithCoder, encodeWithKeyValueArchiver, faultForGlobalID, faultForRawRow, faultForRawRow, fetchTimestamp, finalize, forgetObject, globalIDForObject, hasChanges, initializeObject, initObjectWithCoder, insertedObjects, insertObject, insertObjectWithGlobalID, instancesRetainRegisteredObjects, invalidateAllObjects, invalidateObjectsWithGlobalIDs, invalidatesObjectsWhenFinalized, invokeRemoteMethod, isObjectLockedWithGlobalID, lock, lockObject, lockObjectStore, lockObjectWithGlobalID, locksObjectsBeforeFirstModification, messageHandler, objectForGlobalID, objectsForSourceGlobalID, objectsWithFetchSpecification, objectsWithFetchSpecification, parentObjectStore, processRecentChanges, propagatesDeletesAtEndOfEvent, readResolve, recordObject, redo, refaultAllObjects, refaultObject, refaultObject, refaultObjects, refetch, refreshAllObjects, refreshObject, registeredObjects, removeEditor, reset, retainsRegisteredObjects, revert, saveChanges, saveChanges, saveChangesInEditingContext, setDefaultFetchTimestampLag, setDefaultParentObjectStore, setDelegate, setFetchTimestamp, setInstancesRetainRegisteredObjects, setInvalidatesObjectsWhenFinalized, setLocksObjectsBeforeFirstModification, setMessageHandler, setPropagatesDeletesAtEndOfEvent, setRetainsRegisteredObjects, setSharedEditingContext, setStopsValidationAfterFirstError, setSubstitutionEditingContext, setUndoManager, setUsesContextRelativeEncoding, sharedEditingContext, stopsValidationAfterFirstError, substitutionEditingContext, tryLock, tryToSaveChanges, undo, undoManager, unlock, unlockObjectStore, updatedObjects, usesContextRelativeEncodingprotected static int fakePrimaryKeyCounter
protected NSMutableArray ignoredObjects
protected NSArray entityNamesToGetFromDatabase
public MockEditingContext()
MockObjectStore as parent object store.public void setEntityNamesToGetFromDatabase(NSArray theEntityNamesToGetFromDatabase)
theEntityNamesToGetFromDatabase - array of entity names which should be fetched from the databasepublic com.webobjects.eocontrol.EOObjectStore rootObjectStore()
defaultParentObjectStore.rootObjectStore in class com.webobjects.eocontrol.EOEditingContextEOEditingContext.defaultParentObjectStore()public EOCustomObject createSavedObject(String anEntityName)
insertSavedObject.
Creates a new Custom Object for the specified entity, inserts it into the receiver using insertSavedObject, and returns the new object.anEntityName - the name of entitypublic void insertSavedObject(EOCustomObject anObject)
anObject - the Custom Objectprotected com.webobjects.eocontrol.EOGlobalID assignFakeGlobalIDToObject(EOCustomObject anObject)
insertSavedObject.public void objectWillChange(Object anObject)
insertSavedObject.objectWillChange in interface com.webobjects.eocontrol.EOObservingobjectWillChange in class com.webobjects.eocontrol.EOEditingContextanObject - the object whose state is to be recordedpublic void dispose()
dispose in interface com.webobjects.foundation.NSDisposabledispose in class com.webobjects.eocontrol.EOEditingContextCopyright © 2002 – 2020 Project Wonder.