0

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?

4

1 回答 1

1

如果您正在编写单元测试,那么您真的不想启动 spring 上下文。有一些模拟和自动模拟框架更适合构建虚假依赖项的工作:

http://blog.ploeh.dk/2013/03/11/auto-mocking-container/

http://code.google.com/p/moq/

但是,如果您处于集成测试场景中,则应该查看别名(取决于您的场景,您可以使用专用的别名文件):
http ://www.springframework.net/doc-latest/reference/html/objects .html#d4e596

于 2013-05-30T13:08:53.700 回答