使用JerseyTest 1.19配置ResourceFilter失败,缺少依赖项异常

时间:2019-02-04 20:13:26

标签: java jersey jax-rs jersey-test-framework

我正在尝试使用JerseyTest 1.19.3和GrizzlyTestContainerFactory测试资源。棘手的部分是我的资源使用ResourceFilter。有没有办法使用JerseyTest将这个ResourceFilter配置到测试类中? Test类如下所示:

@Provider
  public static class MockResourceFilterProvider extends
      SingletonTypeInjectableProvider<Context, CustomResourceFilter>
  {
    public MockResourceFilterProvider()
    {
      super(CustomResourceFilter.class, req);
    }
  }

  public TestClass()
  {
    super(new WebAppDescriptor.Builder().initParam(
                                            WebComponent.RESOURCE_CONFIG_CLASS,
                                            ClassNamesResourceConfig.class.getName()
                                        )
                                        .initParam( ClassNamesResourceConfig.PROPERTY_CLASSNAMES,
                                            ClassNamesResourceConfig.PROPERTY_CLASSNAMES,
                                            TestClass.class.getName()
                                            + ';'
                                            + MockResourceFilterProvider.class.getName())
                                            .build());
  }

CustomResourceFilter.java:

  public class CustomResourceFilter extends AbstractResourceFilter
{
  @Inject
  public CustomResourceFilter(
      ObjectFetcher objFetcher
  )
  {
    super(objFetcher);
  }

这里AbstractResourceFilter是ResourceFilter

的抽象实现

使用此代码,出现以下错误:

2019-02-04T19:48:06,286 ERROR [main] com.sun.jersey.spi.inject.Errors - The following errors and warnings have been detected with resource and/or provider classes:
  SEVERE: Missing dependency for constructor public CustomResourceFilter(ObjectFetcher) at parameter index 0

com.sun.jersey.test.framework.spi.container.TestContainerException: com.sun.jersey.spi.inject.Errors$ErrorMessagesException

  at com.sun.jersey.test.framework.spi.container.grizzly2.GrizzlyTestContainerFactory$GrizzlyTestContainer.<init>(GrizzlyTestContainerFactory.java:106)
  at com.sun.jersey.test.framework.spi.container.grizzly2.GrizzlyTestContainerFactory.create(GrizzlyTestContainerFactory.java:77)
  at com.sun.jersey.test.framework.JerseyTest.getContainer(JerseyTest.java:347)
  at com.sun.jersey.test.framework.JerseyTest.<init>(JerseyTest.java:222)
  at org.test.code.TestClass.<init>(TestClass.java:106)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
  at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
  at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
  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.junit.runner.JUnitCore.run(JUnitCore.java:137)
  at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
  at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
  at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
  at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: com.sun.jersey.spi.inject.Errors$ErrorMessagesException

我可以忽略测试类中的ResourceFilter,但是我不确定是否有办法做到这一点。

0 个答案:

没有答案