exception - 在 JerseyTest 构造函数期间使用 Jersey 测试框架产生 NullPointerException

标签 exception pointers testing null jersey

我正在使用 Jersey 测试框架,但是,即使在代码进入我的测试之前,JerseyTest 构造函数也失败了:

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)

我的测试很简单:

@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);
    }
}

实际失败发生在 AbstractJAXBProvider 中调用 setConfiguration 时。 JettyTest 构造函数中失败的实际行是:

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());
}

在我开始分解我的应用程序之前有什么想法吗?

谢谢。 -AP_

最佳答案

我在使用 Spring 3.2.6 和 Jersey 2.6 时遇到了同样的问题。 我通过将 Jersey 升级到 2.9.1 版解决了这个问题。

还要确保您正在使用此依赖项使 spring 3 和 jersey 共存:

<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>

关于exception - 在 JerseyTest 构造函数期间使用 Jersey 测试框架产生 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22240529/

相关文章:

python - 这个 python 装饰器有什么问题?

c - 为什么对于 sqlite3_open 我们使用双指针 ** 而对于 sqlite3_prepare 我们使用指针 *

vue.js - Vue-test-utils 加载依赖 - Vue3

java - 如何立即重新运行失败的 JUnit 测试?

c++ - Release模式工作正常但 Debug模式给出未处理的异常 - 使用 Octave DLL

java.rmi.ConnectException : Connection refused to host: 异常

c++ - 删除从另一个指针分配的指针,我应该再次删除另一个吗?

c++ - 分配空间中的链表?

testing - 创建回归测试

java - 是否有可能找到在 Eclipse 中捕获特定抛出异常的位置?