gpt4 book ai didi

java - 使用 Spring 上下文配置文件的 Jersey 2.4 测试 - DI 不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:07:02 25 4
gpt4 key购买 nike

我正在尝试使用 Spring 配置文件使用 Spring 应用程序上下文配置 Jersey (2.4) 测试。

我的测试类是这样的:

public class OrderControllerTest extends JerseyTest {

@Override
protected Application configure() {
enable(TestProperties.LOG_TRAFFIC);
enable(TestProperties.DUMP_ENTITY);
ApplicationResourceConfig config = new ApplicationResourceConfig();
config.property("contextConfigLocation", "classpath:spring-context.xml");
config.property("spring.profiles.active", "development");
return config;
}

...

ApplicationResourceConfig 类是这样的:

public class ApplicationResourceConfig extends ResourceConfig {

public ApplicationResourceConfig() {
register(RequestContextFilter.class)
.register(OrderController.class);
}

...

OrderController 类是:

@Component
@Path("/order")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class OrderController {

@Autowired
private OrderService orderService;

@PUT
public Response createOrder(String order) {
return Response.ok().build();
}
...

问题是 DI 不工作(orderServicenull)。 DI 在其他测试中运行良好(不是基于 Jersey 的)。我想原因可能是 Spring 配置文件没有正确加载,因为 Spring 日志说:

426  [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver  - Searching for key 'spring.profiles.active' in [systemProperties]
426 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Searching for key 'spring.profiles.active' in [systemEnvironment]
426 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.profiles.active' in any property source. Returning [null]

有没有人可以就这个问题提供建议?

最佳答案

我已经创建了一个示例应用程序如何将 Jersey 2 与 Spring Framework 集成以及如何测试这些应用程序。希望对您有所帮助。

https://github.com/Hylke1982/jersey2-spring-test-example

-- 编辑我还创建了一个新的测试框架,允许您使用 Jersey、Spring Framework 和 Mockito 进行测试。

https://github.com/Hylke1982/jersey-spring-exposed-test-framework-core

关于java - 使用 Spring 上下文配置文件的 Jersey 2.4 测试 - DI 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20049699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com