2

I'm running a test on Hello World trying to follow the Jersey Framework in my spring java program.

I have extended JerseyTest, but I'm getting the error above. The example Jersey gives link doesn't seem to help.

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);
}
}
user269964
  • 159
  • 11

2 Answers2

3

With Jersey 2.26 the dependency has changed to jersey-spring4, but to flush out the answer a little bit more if you don't want to spin up the entire Spring context.

Create the object:

    final ResourceConfig resourceConfig = new ResourceConfig(restResource);

In my case, I just needed an empty context:

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

That was able to allow the JerseyTest to run successfully.

max
  • 471
  • 6
  • 8
  • This doesn't work for me for some reason. Jersey test container still complains it cannot find `applicationContext.xml`. Only solution I found so far is to supply an `applicationContext.xml`. – Nom1fan Jan 05 '22 at 08:44
1

This is going to happen if you have the jersey-spring3 dependency. The default behavior is to look for this applicationContext.xml file (which is your Spring context configuration).

If you want to configure Spring for the test, you can do a couple things.

  1. You could manually create the ApplicationContext and pass it to Jersey

    ApplicationContext context = ...
    return new ResourceConfig(RestService.class)
        .property("contextConfig", context)
    

    If you are using xml configuration, then you would create a ClassPathXmlApplicationContext. If you're using Java config, then you would create an AnnotationConfigApplicationContext.

  2. If you need servlet servlet container support, check out the example in this post

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720