Really for integration testing, not unit testing. You should not normally use the Spring container for unit tests: simply populate your objects in plain NUnit tests!
This supports two modes of populating the test:
This class will normally cache contexts based on a context key:
normally the config locations String array describing the Spring resource
descriptors making up the context. Unless the SetDirty() method
is called by a test, the context will not be reloaded, even across different
subclasses of this test. This is particularly beneficial if your context is
slow to construct, for example if you are using Hibernate and the time taken
to load the mappings is an issue.
If you don't want this behavior, you can override the ContextKey
property, most likely to return the test class. In conjunction with this you would
probably override the GetContext method, which by default loads
the locations specified by the ConfigLocations property.
WARNING: When doing integration tests from within VS.NET, only use assembly resource URLs. Else, you may see misleading failures when changing context locations.
Maintains a cache of contexts by key. This has significant performance benefit if initializing the context would take time. While initializing a Spring context itself is very quick, some objects in a context, such as a LocalSessionFactoryObject for working with NHibernate, may take time to initialize. Hence it often makes sense to do that initializing once.
Normally you won't extend this class directly but rather extend one of its subclasses.
The default implementation populates protected variables if the
You can certainly override this method if you want to totally control how dependencies are injected into 'this' instance.
A flag specifying whether or not dependency checking should be performed for test properties set by Dependency Injection.
The default is true, meaning that tests cannot be run unless all properties are populated.
This is useful in a range of circumstances, allowing the following benefits:
This class is typically very fast, compared to traditional setup/teardown scripts.
If data should be left in the database, call the SetComplete()
method in each test. The "DefaultRollback" property, which defaults to "true",
determines whether transactions will complete by default.
It is even possible to end the transaction early; for example, to verify lazy
loading behavior of an O/R mapping tool. (This is a valuable away to avoid
unexpected errors when testing a web UI, for example.) Simply call the
endTransaction() method. Execution will then occur without a
transactional context.
The StartNewTransaction() method may be called after a call to
EndTransaction() if you wish to create a new transaction, quite
independent of the old transaction. The new transaction's default fate will be to
roll back, unless setComplete() is called again during the scope of the
new transaction. Any number of transactions may be created and ended in this way.
The final transaction will automatically be rolled back when the test case is
torn down.
Transactional behavior requires a single object in the context implementing the IPlatformTransactionManager interface. This will be set by the superclass's Dependency Injection mechanism. If using the superclass's Field Injection mechanism, the implementation should be named "transactionManager". This mechanism allows the use of this superclass even when there's more than one transaction manager in the context.
This superclass can also be used without transaction management, if no IPlatformTransactionManager object is found in the context provided. Be careful about using this mode, as it allows the potential to permanently modify data. This mode is available only if dependency checking is turned off in the AbstractDependencyInjectionSpringContextTests superclass. The non-transactional capability is provided to enable use of the same subclass in different environments.