I’ve got an IoC container doing some complicated object construction when resolving some interfaces. I want to use implementations of these interfaces in my unit/integration tests. Is there anything wrong with resolving these interfaces in the tests using the IoC container or is it expected that one should build up instances manually in this situation?
I’ve got an IoC container doing some complicated object construction when resolving some interfaces.
Share
When we are unit testing a class we are concerned with ‘does the class do what we want it to do’.
Our starting point is a fully constructed instance; how we got there is not a unit testing question though it may be considered an integration testing question.
Say we have,
Where:
IBis an interface forB,ICis an interface forC, andIDis an interface forD.When unit testing
A, the fact thatBusesIDshould be moot (if it is not then we should look at our interfaces. HavingAaccessIB.D.xxx()is not good), all we need to do is provideAwith some implementation (mocked/stubbed) ofIBandIC.As far as the unit tests for
Aare concerned, whether theAinstance is created by hand or by a container is not important. We get the same object either way.As long as we are passing in mocks as the first level dependencies then there is no saving when using a container over creating the object by hand. The saving only happens when we are creating object graphs using the IOC, but if we are doing this then we are into integration testing. This is not necessarily a bad thing but we need to be clear on our goals.
When unit testing the above we create unit testing for
When unit testing
Awe do not need the correct answer fromDto be passed throughBup toA.All we care is that the logic in A works as expected, say,
AcallsIB.x()with the parameters y and z and returns the result ofIB.x(). If we are checking that we get the correct answer (say, one which depends on logic inD) then we are past unit testing and into integration testing.Bottom Line
It does not matter whether or not the unit under test was created by an IOC container or by hand as long as we are properly isolating the unit under test. If we are using the container to create an object graph then the odds are good that we are into integration testing (and/or have problems with too much coupling between classes –
AcallingIB.D.xxx())Mocking for Integration Tests
Caveat: Some of this following is dependent upon the IOC container in use. When using Unity, the last registration ‘wins’. I do not know that this holds true for others.
In the minimalist system under question we have
Bis our ‘leaf’. It talks to the outside world (say, reads from a network stream).When Integration testing, we want to mock this.
For the live system, we set up the ServiceLocator using
CreateContainerCore().This includes the registration of the ‘live’ implementation of
IB.When executing integration tests that require a mocked version of
IBwe set up the container usingCreateContainerWithMockedExternalDependencies()which wrapsCreateContainerCore()and registering a mocked object forIB.The code below is heavily simplified but the shape extends out to as many classes and dependencies as required. In practice, we have a base test class that aids setting up the service locator, mocking/stubbing classes, accessing the mocks for verification purposes and other house keeping (e.g.so that each test doesn’t need to explicitly set the ServiceLocator provider)