22

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 }
luboskrnac
  • 23,973
  • 10
  • 81
  • 92
Dominic
  • 735
  • 2
  • 8
  • 20

2 Answers2

36

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() { }
luboskrnac
  • 23,973
  • 10
  • 81
  • 92
  • Just for reference: setUp() and tearDown() are the names autogenerated by Netbeans for the Before and After methods. Renaming these solved the problem for me (with NB 8.1 and Jersey 2.22) so this is still relevant. – tekHedd May 24 '16 at 21:44
  • Good to know, wasn't aware of that it is generated. Not using Netbeans. That explains higher interest in this question. – luboskrnac May 25 '16 at 13:56
  • What a great catch! I was going nuts on this one! – sschrass Feb 20 '17 at 16:37
19

I came here because I was using JUnit 5 and it seems that it wasn't seeing the @Before and @After annotations on the JerseyTest setup/tearDown methods. I had to override them and use the new JUnit 5 annotations

public class MyResourceTest extends JerseyTest {
    @BeforeEach
    @Override
    public void setUp() throws Exception {
        super.setUp();
    }

    @AfterEach
    @Override
    public void tearDown() throws Exception {
        super.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);
    }
}
Victor
  • 3,395
  • 1
  • 23
  • 26
  • 3
    This is the corresponding bug report (open since 2017-09): https://github.com/eclipse-ee4j/jersey/issues/3662 – simon04 Jun 17 '20 at 12:12