user2410988
user2410988

Reputation: 1

Spring .NET setting a property value in config without instantiating the object

I am writing a TestClass let's say "ServiceTest" which has a property called ExampleService

public IService ExampleService { get; set;}

I am using the ExampleService inside my test method and invoking some operations on it. I need to instantiate the IService using spring.config. But as it is a Test class I do not want to create instance of ServiceTest inside my spring config. I know how to do that using tag.

<object id="MyService" type="MyTestProject.ServiceTest, MyTestProject">
    <property name="ExampleService " ref="SomeAssembly.ServiceClass"/>
</object>

But I just want to set the value of ExampleService in my app.config so that it gets instantiated and set for the variable ExampleService.

How can I do this?

Upvotes: 0

Views: 474

Answers (1)

Andreas
Andreas

Reputation: 5309

If you are writing a unit test, you really do not want to start a spring context. There are mocking and auto-mocking frameworks out there that better suits the job of building a fake dependency:

http://blog.ploeh.dk/2013/03/11/auto-mocking-container/
and
http://code.google.com/p/moq/

If you are however in a integration test scenario than you should have a look at aliases (depending on your scenario you could use a dedicated alias file):
http://www.springframework.net/doc-latest/reference/html/objects.html#d4e596

Upvotes: 1

Related Questions