来自org.glassfish.jersey.test.JerseyTest的NPE @Test(groups = {" integration"})通过TestNG

时间:2014-11-01 06:30:09

标签: jersey testng

我有一个gradle项目带来Jersey 2.13,通过TestNG进行测试,并利用Jersey测试框架。通过@Test注释作为普通单元测试运行的任何测试运行正常。如果我尝试通过@Test将泽西测试放入TestNG组(groups = {" integration"}),测试将失败,NPE来自org.glassfish.jersey.test.JerseyTest。测试不利用Jersey测试框架不能正确执行此问题作为@Test的一部分(groups = {" integration"})。任何想法为什么TestNG无法在群组限制下正确启动此容器?

以下是该项目的执行部分......

的build.gradle

dependencies {
    ...
    compile 'org.glassfish.jersey.core:jersey-server:2.13'
    compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.13'
    compile 'org.glassfish.jersey.media:jersey-media-json-jackson:2.13'
    ...        
    testCompile 'org.testng:testng:6.8.8'
    testCompile 'org.glassfish.jersey.test-framework:jersey-test-framework-core:2.13'
    testCompile 'org.glassfish.jersey.test-framework.providers:jersey-test-framework-provider-jetty:2.13'
}

test {
    useTestNG() { excludeGroups 'integration' }
}

task integrationTest(type: Test, dependsOn: 'test') {
    useTestNG() { includeGroups 'integration' }
}

失败的集成测试......

public class JerseyTestNGTest extends JerseyTestNg.ContainerPerClassTest {
  @Path("hello")
  public static class HelloResource {
    @GET
    public String getHello() { return "Hello World!"; }
  }

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

  @Test
  public void notIntegrationTest() {
    // works like a champ...
    doIt();
  }

  @Test(groups = {"integration"})
  public void integrationTest() {
    // NPEs out the wazoo
    doIt();
  }

  private void doIt() {
    final String hello = target("hello").request().get(String.class);
    assertEquals("Hello World!", hello);
  }
}
泪流满面......

$ gradle integrationTest
...
:integrationTest

Gradle test > jerseywtf.JerseyTestNGTest.integrationTest FAILED
    java.lang.NullPointerException at JerseyTestNGTest.java:29

2 tests completed, 1 failed
:integrationTest FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':integrationTest'.
> There were failing tests. See the report at: file:///Users/robert.kuhar/work/jerseywtf/build/reports/tests/index.html
...    
BUILD FAILED

实际的堆栈跟踪...

java.lang.NullPointerException
    at org.glassfish.jersey.test.JerseyTest.target(JerseyTest.java:566)
    at org.glassfish.jersey.test.JerseyTest.target(JerseyTest.java:580)
    at jerseywtf.JerseyTestNGTest.doIt(JerseyTestNGTest.java:29)
    at jerseywtf.JerseyTestNGTest.integrationTest(JerseyTestNGTest.java:40)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    ...
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

该怎么做?

整个项目都在github上,如果您有兴趣看到它运行,或者视情况而失败:https://github.com/robertkuhar/JerseyWTF

0 个答案:

没有答案