Java Jersey 2.4使用Spring上下文概要文件进行测试-DI不工作
我正在尝试使用Spring应用程序上下文和Spring概要文件配置Jersey(2.4)测试 我的测试课程是:Java Jersey 2.4使用Spring上下文概要文件进行测试-DI不工作,java,spring,rest,junit,jersey-2.0,Java,Spring,Rest,Junit,Jersey 2.0,我正在尝试使用Spring应用程序上下文和Spring概要文件配置Jersey(2.4)测试 我的测试课程是: public class OrderControllerTest extends JerseyTest { @Override protected Application configure() { enable(TestProperties.LOG_TRAFFIC); enable(TestProperties.DUMP_ENTITY
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;
}
...
public class ApplicationResourceConfig extends ResourceConfig {
public ApplicationResourceConfig() {
register(RequestContextFilter.class)
.register(OrderController.class);
}
...
其中ApplicationResourceConfig
类是:
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;
}
...
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不工作(orderService
为null
)。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集成,以及如何测试这些应用程序。我希望这会有帮助 --编辑 我还创建了一个新的测试框架,允许您使用Jersey、Spring框架和Mockito进行测试
可能重复@nobeh您仔细阅读了我的问题吗?DI在其他测试中运行良好(不是基于Jersey的),我可能弄错了。如果问题是将Jersey 2+与Spring Java配置集成,请看一看我已经读过的网页,实际上没有发布解决方案。这可能是Jersey/Spring集成-属性表单资源配置(即
Spring.profiles.active
)中的问题,除了contextConfigLocation
对Spring不可见之外(当然,它们没有设置为系统属性)。请您对现有问题进行评论或创建一个新的问题好吗?谢谢,但问题是使用Spring概要文件在Jersey测试中配置Spring上下文