ServiceLocator in Unit Tests

Topics: Prism v4 - WPF 4
Jul 25, 2013 at 11:18 AM
The ServiceLocator has been used within a class to create a service, as this service has not been passed through in the constructor.

The ServiceLocator is just used statically to create this service (it has not been passed through). Is this Unit Test-able?
Jul 25, 2013 at 7:03 PM

Based on my understanding, the ServiceLocator was designed so that it could be used in tests. Basically, when you a request an instance to the ServiceLocator, it will delegate the request to its locator provider in order to create the corresponding object. Hence, you will only need to create a simple mock locator provider that would return the corresponding mocks for the requested objects. This locator provided can be set using the SetLocatorProvider static method. You can find a example of this in the UnityBootstrapper:
protected override void ConfigureServiceLocator()
    ServiceLocator.SetLocatorProvider(() => this.Container.Resolve<IServiceLocator>());
You can find more information about configuring the ServiceLocator in their codeplex site:


Damian Cherubini