Jersey / Spring / Grizzly Integration Tests总是返回500 - 没有细节

时间:2017-08-01 19:22:27

标签: java grizzly jersey-test-framework spring-jersey

我正在尝试为使用Jersey和一些Spring DI的应用程序编写一些集成测试。测试总是以500的形式回归,没有真正的解释。 Grizzly似乎开始正常,启动我的休眠连接就好了。以下是错误跟踪以及所测试的所有相关文件。我认为这可能是Grizzly和配置的问题,但我找不到任何似乎有用的东西,包括切换到webappcontext。

<div></div>

测试类

javax.ws.rs.InternalServerErrorException: HTTP 500 Request failed.
    at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:1032)
    at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:819)
    at org.glassfish.jersey.client.JerseyInvocation.access$700(JerseyInvocation.java:92)
    at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:701)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:697)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:420)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:316)
    at com.heapo.test.intergration.resources.UserResourceTest.test_getUser(UserResourceTest.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Web.xml - 工作 - 应用程序在Tomcat下运行正常,我可以通过邮递员访问整个API。

package com.sample.test.intergration.resources;

import static org.junit.Assert.assertEquals;

import javax.ws.rs.core.Application;

import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.TestProperties;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.sample.models.User;
import com.sample.rest.config.SampleApplication;
import com.sample.spring.config.SpringSecurityConfig;


public class UserResourceTest extends JerseyTest {

    @Test
    public void test_getUser() {
        final User hello = target("user/1")
                                .request()
                                .header("Authorization", "BEARER xxx")
                            .get(User.class);
   }

   @Override
   protected Application configure() {
       enable(TestProperties.LOG_TRAFFIC);
       enable(TestProperties.DUMP_ENTITY);

        ResourceConfig rc = new ProgramProApplication();
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringSecurityConfig.class);
        rc.property("contextConfig", ctx);

        return rc;
    }
}

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>Sample API</display-name>
    <servlet>
        <servlet-name>Sample API</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
             <param-name>javax.ws.rs.Application</param-name>
             <param-value>com.sample.rest.config.SampleApplication</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
        </init-param>        
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Sample API</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.sample.spring.config.SpringSecurityConfig</param-value>
    </context-param>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

1 个答案:

答案 0 :(得分:2)

经过几天的试验和错误,几乎没有任何文档和其他示例拼凑在一起已经过时了,这是最终为我工作的。希望它能帮助别人。

测试类

package com.sample.test.intergration.resources;

import static org.junit.Assert.assertEquals;

import javax.ws.rs.core.Application;

import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.TestProperties;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.sample.models.User;
import com.sample.rest.config.SampleApplication;
import com.sample.spring.config.SpringSecurityConfig;


public class UserResourceTest extends JerseyTest {

    @Test
    public void test_getUser() {
        final User hello = target("user/1")
                            .request()
                            .header("Authorization", "BEARER xxx")
                            .get(User.class);
   }

    @Override
    protected DeploymentContext configureDeployment() {
        return ServletDeploymentContext
                .forServlet(new ServletContainer(configureRC()))
                .addListener(ContextLoaderListener.class)
                .contextParam(ContextLoader.CONFIG_LOCATION_PARAM, SpringSecurityConfig.class.getPackage().getName())
                .contextParam(ContextLoader.CONTEXT_CLASS_PARAM, AnnotationConfigWebApplicationContext.class.getName())
                .addFilter(DelegatingFilterProxy.class, "springSecurityFilterChain")
                .build();       
    }   

    @Override
    protected TestContainerFactory getTestContainerFactory() {
        return new GrizzlyWebTestContainerFactory();
    }       
}