挂毯 +弹簧单元测试
我有下一种情况 - 我有一个服务依赖于 @Inject Tapestry 注释,该注释是使用 Spring + Tapestry 集成通过 Spring 上下文注入的。我想使用测试依赖项注入而不是真正的依赖项注入来对该服务进行单元测试。我不知道如何让 Tapestry 使用测试 Spring 上下文并注入到我的服务实例中来测试我的模拟依赖项,使用 testify 或其他东西,请帮忙!
I have a next situation - I have a service which have dependency with @Inject tapestry annotation which is injected through Spring context using Spring + Tapestry integration. I'd like to unit test this service using using test dependency injection instead of real one. I don't how to make Tapestry use test Spring context and inject into my service instance in test my mocked dependency, use testify or something, please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Tapestry 有一个内置机制,可以使用 PageTester 测试其页面类。它还具有使用 Selenium 的集成套件,如此处所述。
但我更喜欢以不同的方式测试 Tapestry - spring。
Tapestry has a inbuilt mechanism for testing its page classes using PageTester. Also it has a integration suit using Selenium as explained here.
But I prefer to test Tapestry - spring in a different way.
如果我们在这里讨论单元测试,
您所需要做的就是在服务代码中为您的依赖项(您 @Injecte'ed 的那个)定义一个公共 setter 方法。当您编写测试时,只需使用 setter 并将引用传递给您的 Mock/Stub/Fake 而不是真正的实现。
如果您正在编写集成测试,
Spring 提供了一组很好的注释,允许您使用自定义上下文配置,使您的测试方法具有事务性(这对于测试与数据库交互的(Tapestry 或 Spring)服务非常有用)等。
您的集成测试类如下所示:
If we are talking about Unit Testing here
All you need to do is define a public setter method for your dependency (the one you @Injecte'ed) in the Service code. When you write your test, just use the setter and pass the reference to your Mock/Stub/Fake instead of the real implementation.
If you are writing an Integration Test
Spring provides a nice set of annotations which allow you to use a custom context configuration, make your test methods transactional (which is really useful for testing (Tapestry or Spring) services which interact with database), etc.
Here's how your Integration Test class could look like: