public class MockEditingContext extends 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.
EOEditingContext._EventLoggingEnabler, EOEditingContext.Delegate, EOEditingContext.EditingContextEvent, EOEditingContext.Editor, EOEditingContext.MessageHandler
EOKeyValueArchiving._NullValueSupport, EOKeyValueArchiving._NumberSupport, EOKeyValueArchiving._TimestampSupport, EOKeyValueArchiving.Awaking, EOKeyValueArchiving.FinishInitialization, 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
_doAssertLock, _doAssertLockInitialized, _userInfo, _wasDisposed, DeletedKey, InsertedKey, InvalidatedAllObjectsInStoreNotification, InvalidatedKey, ObjectsChangedInStoreNotification, UpdatedKey
Constructor and Description |
---|
MockEditingContext()
Constructs a MockEditingContext.
|
Modifier and Type | Method and Description |
---|---|
protected 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 . |
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, usesContextRelativeEncoding
_checkAssertLock, _resetAssertLock, _suppressAssertLock, setUserInfo, setUserInfoForKey, userInfo, userInfoForKey
protected 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 EOObjectStore rootObjectStore()
defaultParentObjectStore
.rootObjectStore
in class EOEditingContext
EOEditingContext.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 EOGlobalID assignFakeGlobalIDToObject(EOCustomObject anObject)
insertSavedObject
.public void objectWillChange(Object anObject)
insertSavedObject
.objectWillChange
in interface EOObserving
objectWillChange
in class EOEditingContext
anObject
- the object whose state is to be recordedpublic void dispose()
dispose
in interface NSDisposable
dispose
in class EOEditingContext
Copyright © 2002 – 2024 Project Wonder.