filenotfoundexception运行jerseyTest - 缺少applicationContext.xml

时间:2017-08-09 13:04:57

标签: java spring jersey-test-framework

我在Hello World上运行测试,试图在我的spring java程序中遵循Jersey Framework。

我已经扩展了JerseyTest,但我收到了上面的错误。泽西提供link的示例似乎没有帮助。

public class SimpleTest extends JerseyTest {



@Override
protected Application configure() {
    return new ResourceConfig(RestService.class);
}

@Test
public void test() {
    final String hello = target("\service\hello").request().get(HelloModel.class);
    assertEquals("Hello World!", hello);
}
}

2 个答案:

答案 0 :(得分:0)

如果您拥有jersey-spring3依赖项,就会发生这种情况。默认行为是查找此applicationContext.xml文件(这是您的Spring上下文配置)。

如果要为测试配置Spring,可以执行一些操作。

  1. 您可以手动创建ApplicationContext context = ... return new ResourceConfig(RestService.class) .property("contextConfig", context) 并将其传递给Jersey

    ClassPathXmlApplicationContext

    如果您使用的是xml配置,则可以创建AnnotationConfigApplicationContext。如果您正在使用Java配置,那么您将创建一个String

  2. 如果您需要servlet servlet容器支持,请查看solution

  3. 中的示例

答案 1 :(得分:0)

使用Jersey 2.26时,依赖关系已更改为jersey-spring4,但如果您不想想要启动整个Spring上下文,请更多地清除答案。

创建对象:

    final ResourceConfig resourceConfig = new ResourceConfig(restResource);

就我而言,我只需要一个空的背景:

    final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.refresh();
    resourceConfig.property("contextConfig", context);

这样就可以让JerseyTest成功运行。