3

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

我的测试课是这样的:

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]

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

4

1 回答 1

1

我创建了一个示例应用程序,如何将 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

于 2013-11-19T20:20:34.800 回答