挂毯 +弹簧单元测试

发布于 2024-12-13 01:56:45 字数 200 浏览 3 评论 0原文

我有下一种情况 - 我有一个服务依赖于 @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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风疏影 2024-12-20 01:56:45

Tapestry 有一个内置机制,可以使用 PageTester 测试其页面类。它还具有使用 Selenium 的集成套件,如此处所述。

但我更喜欢以不同的方式测试 Tapestry - spring。

  1. 从 Spring 应用程序上下文中获取服务的引用。
  2. 使用页面类的构造函数并初始化服务。通过这种方式,您可以通过注入服务来完成挂毯的工作。
  3. 现在您已准备好直接测试页面类中的方法。

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.

  1. Get the reference of the services from spring application context.
  2. Use a constructor of the page class and initialize the services. This way you does the job of tapestry by injecting the services.
  3. Now you are all set to test the methods inside the page classes directly.
颜漓半夏 2024-12-20 01:56:45

如果我们在这里讨论单元测试,

您所需要做的就是在服务代码中为您的依赖项(您 @Injecte'ed 的那个)定义一个公共 setter 方法。当您编写测试时,只需使用 setter 并将引用传递给您的 Mock/Stub/Fake 而不是真正的实现。

public class SomeServiceWithDependecies {
    @Inject
    private SomeService _myService;

    // Use this setter in your Unit Test to set _myService to Mock/Stub/Fake implementation
    public void setMyService(SomeService someService) {
      _myService = someService;
    }
}

如果您正在编写集成测试,

Spring 提供了一组很好的注释,允许您使用自定义上下文配置,使您的测试方法具有事务性(这对于测试与数据库交互的(Tapestry 或 Spring)服务非常有用)等。

您的集成测试类如下所示:

@ContextConfiguration(locations = { "/applicationContext-test.xml" })
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class SomeServiceWithDaoDependeciesITCase extends AbstractTransactionalTestNGSpringContextTests {
    // Make sure your applicationContext is properly written
    // and contains <context:annotation-config />
    @Autowired
    private SomeDAO _someDAO;

    // .. your transactional test methods go here
}

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.

public class SomeServiceWithDependecies {
    @Inject
    private SomeService _myService;

    // Use this setter in your Unit Test to set _myService to Mock/Stub/Fake implementation
    public void setMyService(SomeService someService) {
      _myService = someService;
    }
}

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:

@ContextConfiguration(locations = { "/applicationContext-test.xml" })
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class SomeServiceWithDaoDependeciesITCase extends AbstractTransactionalTestNGSpringContextTests {
    // Make sure your applicationContext is properly written
    // and contains <context:annotation-config />
    @Autowired
    private SomeDAO _someDAO;

    // .. your transactional test methods go here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文