Dominic Dominic - 4 months ago 85
Java Question

JerseyTest and JUnit throws NullPointerException

I have some problems with the jersey test framework. If i use the @Before and @After annotations, then the target method throws a NullPointerException.
I thought JerseyTest works with JUnit? Where is my problem?


  • Jersey: 2.12

  • JUnit: 4.11



Code that fails:

public class MyResourceTest extends JerseyTest {
@Before
public void setUp() { }

@After
public void tearDown() { }

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

@Test
public void SHOULD_RETURN_BAD_REQUEST() throws IOException {
System.out.println(target("myPath"));
assertEquals(1, 1);
}
}


Result:


java.lang.NullPointerException at
org.glassfish.jersey.test.JerseyTest.target(JerseyTest.java:566) at
org.glassfish.jersey.test.JerseyTest.target(JerseyTest.java:580) at
foo.bar.MyResourceTest.SHOULD_RETURN_BAD_REQUEST(MyResourceTest.java:43)


Code that works:

public class MyResourceTest extends JerseyTest {
@Override
protected Application configure() {
return new ResourceConfig(MyResource.class);
}

@Test
public void SHOULD_RETURN_BAD_REQUEST() throws IOException {
System.out.println(target("myPath"));
assertEquals(1, 1);
}
}


Result:

JerseyWebTarget { http://localhost:9998/myPath }

Answer

Your methods seem to override some important initialization made in parent JerseyTest. Try to name them differently. E.g.:

@Before
public void setUpChild() { }

@After
public void tearDownChild() { }
Comments