【问题标题】:Jersey 2.x + JUnit + Spring Managed Resource?Jersey 2.x + JUnit + Spring 托管资源?
【发布时间】:2014-01-14 16:58:00
【问题描述】:

我进行了很多搜索,但找不到使用 Spring 托管资源 bean 的 Jersey 2.x junit 测试示例。

以下是我正在尝试的,但它不起作用。它错误的是 MySpringManagedResource 没有无参数构造函数,这表明它只是忽略了 Spring。

public class MyServiceUnitTest extends JerseyTest {
    @Override
    protected Application configure() {
        return new ResourceConfig(MySpringManagedResource.class).
                register(org.glassfish.jersey.server.spring.SpringComponentProvider.class).
                register(MySpringConfigBean.class);
    }

    ...
}

【问题讨论】:

  • 查看我的回答to this question。您可以为 Jersey 提供您自己的类,该类负责实例化资源(或作为 Spring 管理的 bean 从 Spring 获取它们)。虽然我的回答涉及接口,但我不明白为什么它也不适用于类。

标签: spring unit-testing junit jersey


【解决方案1】:

感谢 John R. 引导我找到解决方案。如果可以的话,我会将您的答案标记为正确答案。我最终使用了这个:

@Override
protected Application configure() {
    ApplicationContext context = new AnnotationConfigApplicationContext(MyConfigClass.class);
    MyJerseyResource restResource = context.getBean(MyJerseyResource.class);

    return new ResourceConfig().registerInstances(restResource);
}

【讨论】:

    猜你喜欢
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    相关资源
    最近更新 更多