java - 运行 Jersey 测试时初始化 Spring beans

标签 java spring spring-boot jersey-2.0 spring-annotations

我正在使用最新的 Spring 和 Jersey 测试框架。 我所有的资源都使用 Jersey 。

我有 spring bean,它们正在由 @Service 注释初始化。 每个服务将自己注册到一个客户 ServiceContainer,它是一个单例 bean。

在我的资源中,我从容器中获取服务并使用注释。 我正在使用 Jersey 框架运行单元测试。如何使用 @Service 注释来初始化这些服务。我没有在我的资源中使用 @Autowire/@Inject 注释。

我查看了几个类似的案例,但没有任何帮助,有什么想法吗?

最佳答案

这是对我有用的:

    @Override
protected DeploymentContext configureDeployment() {
    ResourceConfig config = new ResourceConfig();
    config.packages("com.my.application");
    return ServletDeploymentContext
            .forServlet(new ServletContainer(config))
            .addListener(ContextLoaderListener.class)
            .contextParam("contextConfigLocation", "classpath:applicationContext.xml")
            .build();

}

关于java - 运行 Jersey 测试时初始化 Spring beans,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36661807/

相关文章:

java - java中索引越界异常

javascript - 需要通过javascript与浏览器通信的java applet的替代方案

java - 使用spring boot和spring-boot-maven-plugin生成war时排除application.properties

java - 在@Profile 中使用通配符

java - jackson 的 InvalidDefinitionException

java - ActionScript3 和 Java 加密/解密

java - Dao,遗留代码中的服务层

java - 如果我没有标记为 @EnableJms 注释的类,spring 如何查找 @EnableJms 方法?

java - RestTemplate.exchange() 不编码 '+' ?

java - 如何从resilience4j TimeLimitter 抛出自定义异常?