简体   繁体   English

Jersey-test-framework无用的回溯JerseyTest

[英]Jersey-test-framework unhelpful traceback JerseyTest

I have a test with an Error that gives no good information. 我有一个错误没有给出良好信息的测试。 The only piece of the stack that involves my code is the method call in the test which fails. 堆栈中涉及我的代码的唯一部分是测试中的方法调用,该方法调用失败。

-------------------------------------------------------------------------------
Test set: com.example.ExampleServiceTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.258 sec <<< FAILURE!
testGetNoHeaders(com.example.ExampleServiceTest)  Time elapsed: 0.256 sec  <<< ERROR!
javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error
    at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:957)
    at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:784)
    at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:91)
    at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:672)
    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:424)
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:668)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:397)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:297)
    at com.example.ExampleServiceTest.testGet(ExampleServiceTest.java:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)

Test class: 测试类别:

package com.example;

import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.mvc.mustache.MustacheMvcFeature;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;

import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Application;


public class FakeSiteminderServiceTest extends JerseyTest {

    @Override
    protected Application configure() {

        final ResourceConfig rc = new ResourceConfig().property(
                MustacheMvcFeature.TEMPLATE_BASE_PATH, "templates"
        ).register(
                MustacheMvcFeature.class
        ).packages("com.example");
        return rc;
    }

    @Test
    public void testGet() {
        System.out.println("111111111111111111111111111111111111");
        WebTarget t = target("/api/url");
        System.out.println(t.toString());
        Invocation.Builder b = t.request();
        System.out.println(b.toString());
        String respString = b.get(String.class); // <-- ERROR IS HERE
        System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
        System.out.println(respString);
    }
}

The class I'm trying to test works fine when I run it with grizzly and render it manually with a browser. 当我尝试使用grizzly运行该类并使用浏览器对其进行手动呈现时,我要测试的类工作正常。

@Path("url") public class FakeSiteminderService { @Path(“ url”)公共类FakeSiteminderService {

@GET
@Produces(MediaType.TEXT_HTML)
@Template(name = "/foo")
public Map<String, String> get(
        @Context HttpHeaders hh
) {
    Map<String, String> ctx = new HashMap<>();

    if (hh.getRequestHeader("FULLNAME") != null) {
        ctx.put("FULLNAME", hh.getRequestHeader("FULLNAME").get(0));
        ctx.put("EMAIL", hh.getRequestHeader("EMAIL").get(0));
    }
    return ctx;
}

You may be able to get some more information out or a different traceback if you put some code like this somewhere in your package: 如果将这样的代码放在程序包中的某个位置,则可以获取更多信息或进行其他追溯:

@Provider
public class CustomExceptionMapper  implements
        ExceptionMapper<Throwable> {

    @Override
    public Response toResponse(Throwable ex) {
        System.out.println("EXCEPTIONMAPPER!!!!!!!!!!!!!!!!!!!!");
        System.out.println(Exceptions.getStackTraceAsString(ex)); // <--
        return Response.status(500).entity(
                Exceptions.getStackTraceAsString(ex)
        ).type("text/plain").build();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM