我正在为其余控制器进行单元测试,这只是较大应用程序的一小部分。 理想情况下,我想使用模拟框架来确保测试是单一的。 我会嘲笑经理和道。 但是,如果我们处于测试环境或应用程序环境中,则需要为其他控制器类配置不同的配置,以使其使用不同的管理器。 mock在context-test.xml中定义。
这是我迄今为止所做的: 测试RestController
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(locations = "classpath:/META-INF/spring/context-test.xml") @WebIntegrationTest public class MyRestControllerTest extends AbstractTransactionnalTest { @Autowired private IManager manager; @Test // my unit tests }RestController
@RestController @SpringApplicationConfiguration(locations = {"classpath:/META-INF/spring/context-test.xml", "classpath:/META-INF/spring/context-application.xml"}) @RequestMapping("/me") class MyRestController { @Autowired private IManager manager; // Content of my controller }迄今为止我的解决方案的主要问题是: - 我不知道如何告诉RestController使用哪个上下文。 (我一次只想使用一个上下文)
有没有更好的解决方案来做到这一点?
I am doing unit tests for a rest controller, which is only a small part of a bigger application. Ideally I would like to use a mocking framework to ensure that the test are unitary. I would mock the manager and the dao. However that would require to have different configurations for the rest controller class that make him use a different manager depending if we are in test context or in application context. The mocks are defined in context-test.xml.
This is what I have done so far : Test RestController
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(locations = "classpath:/META-INF/spring/context-test.xml") @WebIntegrationTest public class MyRestControllerTest extends AbstractTransactionnalTest { @Autowired private IManager manager; @Test // my unit tests }RestController
@RestController @SpringApplicationConfiguration(locations = {"classpath:/META-INF/spring/context-test.xml", "classpath:/META-INF/spring/context-application.xml"}) @RequestMapping("/me") class MyRestController { @Autowired private IManager manager; // Content of my controller }The main issue with my solution so far : - I dont know how to tell the RestController wich context to use. (I only want to use one context at a time)
Is there a better solution to do this ?
最满意答案
我同意@chrylis。 我想这里的问题可能是你的班级设计。
如果你的MyRestController类依赖于知道传入哪个上下文,看起来这将是一个Spring / DI反模式。 DI的整个意义在于,课堂“被动地”首先以正确的行为处理上下文。
任何注入的对象都应该简单地通过注入上下文来正确创建/处理。
I agree with @chrylis. The problem here I think may be your class design.
If your MyRestController class is dependent on knowing which context is passed in, seems like this would be a Spring/DI anti-pattern. The whole point of DI is that the class "passively" handles the context with correct behavior in the first place.
Any injected objects should simply be created/handled correctly by the injecting context.
更多推荐
发布评论