Using Jersey Test Framework produces NullPointerException during JerseyTest constructor

I am using Jersey Test Framework, however, even before the code gets to my test, the JerseyTest constructor fails with:

Mar 06, 2014 6:40:44 PM org.glassfish.jersey.server.ApplicationHandler initialize
INFO: Initiating Jersey application, version Jersey: 2.6 2014-02-18 21:52:53...

A MultiException has 3 exceptions.  They are:
1. java.lang.NullPointerException
2. java.lang.IllegalStateException: Unable to perform operation: method inject on com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$App
3. java.lang.IllegalStateException: Unable to perform operation: create on org.glassfish.jersey.message.internal.MessageBodyFactory
MultiException stack 1 of 3
java.lang.NullPointerException
    at com.sun.jersey.core.provider.jaxb.AbstractJAXBProvider.setConfiguration(AbstractJAXBProvider.java:112)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.glassfish.hk2.utilities.reflection.ReflectionHelper.invoke(ReflectionHelper.java:1017)
    at org.jvnet.hk2.internal.ClazzCreator.methodMe(ClazzCreator.java:375)
    at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:428)
    at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:456)
    at org.jvnet.hk2.internal.PerLookupContext.findOrCreate(PerLookupContext.java:69)
    at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2445)

My test is very simple:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/applicationContext-test.xml"})
public class BidAPITest extends JerseyTest {

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

    @Test
    public void testCreate() {
        final BidDocument
            bid =
                target(BidAPI.PATH)
                    .request()
                    .post(
                        Entity.entity("", MediaType.APPLICATION_JSON_TYPE),
                        BidDocument.class
                    );

        System.out.println(bid);
    }
}

The actual failure occurs while setConfiguration is being invoked in AbstractJAXBProvider. The actual line which fails in the JettyTest constructor is:

public ComponentModelValidator(ServiceLocator locator) {
    validators = Lists.newArrayList();
    validators.add(new ResourceValidator());
    /* -FAILS-> */ validators.add(new RuntimeResourceModelValidator(locator.getService(MessageBodyWorkers.class)));
    validators.add(new ResourceMethodValidator(locator));
    validators.add(new InvocableValidator());
}

Any ideas before I start ripping apart my application?

Thanks. -AP_

Answers


I ran into the same problem using Spring 3.2.6 & Jersey 2.6. I solved it by upgrading Jersey to version 2.9.1.

Also make sure you are using this dependency to make spring 3 & jersey coexist :

<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-spring3</artifactId>
    <version>2.9.1</version>
    <exclusions>
    <exclusion>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
    </exclusion>
     </exclusions>
</dependency>

Need Your Help

How can I ensure my process never locks another process out of a file?

c++ c windows winapi file-locking

I have a Windows process that runs in the background and periodically backs up files. The backup is done by uploading the file to a server.

Swift location comes back nil

ios swift mapkit

This code was functioning earlier, and now it keeps crashing because the location is coming back as nil. I'm not sure where this error is coming from or how to fix it. From what I can tell, my c...