【问题标题】:Testing Jersey endpoints with members annotated with springs @Value使用带有弹簧 @Value 注释的成员测试 Jersey 端点
【发布时间】:2016-11-11 16:48:43
【问题描述】:

我正在尝试使用 Jersey,它与 Spring 集成,以便在我们的 spring web-app 中拥有 jax-rs 端点。

我遇到的问题是,使用 @Value 注释的应用程序成员没有填充到我们的单元测试中。

用@Autowired 填充的那些工作正常,如果我为字符串创建一个setter 方法并用@Value 注释它也可以工作。

让我恼火的是,直接用@Value 注释的成员在测试中不起作用,但在部署到我们的tomcat 中时起作用。

这是我们的测试配置的问题吗?或者这是某个地方的已知问题?

这些是我认为的相关依赖:

<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-spring3</artifactId>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.test-framework</groupId>
    <artifactId>jersey-test-framework-core</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.test-framework.providers</groupId>
    <artifactId>jersey-test-framework-provider-jetty</artifactId>
    <scope>test</scope>
</dependency>

一个小项目在这里举例说明了这个问题: https://github.com/alexanderkjall/jersey-jetty-interaction-example/blob/master/src/main/java/no/hackeriet/bugs/SpringRequestResource.java

【问题讨论】:

  • 可以添加一些相关代码吗?
  • 我不能真正分享工作中的代码库,但我在这里重现了这个问题:github.com/alexanderkjall/jersey-jetty-interaction-example/blob/… 我猜这是预期的行为,但我不明白为什么(使用 'mvn jetty 运行代码:run')
  • 开始怀疑这是否是一个错误
  • 这是一个bug吗,你可以用mockito来模拟你的对象,然后做单元测试

标签: java spring jersey jersey-test-framework


【解决方案1】:

似乎您可能缺少组件扫描,添加后对我来说效果很好。

<context:component-scan base-package="no.hackeriet.bugs" /> 

但我不确定它在没有组件扫描的情况下如何在 tomcat 中工作,这让我很困惑。

【讨论】:

  • 我也发现了这一点,我不想在我的单元测试中使用组件扫描,因为我想明确控制测试中的上下文。我在这里打开了一个关于球衣的问题:java.net/jira/browse/JERSEY-3183
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-28
相关资源
最近更新 更多