dimanche 26 juin 2016

REST API test using Jersey Test Framework

My REST API defined in REST.class is as below,

@GET
@Path("/somepath")
@Produces(MediaType.APPLICATION_JSON)
public Response getSomething() {
    List<SomeObject> someList = new ArrayList<>();
    //.. based on some logic Adding SomeObject to the someList
    return Response.ok(someList).build();
}

I am trying to run Unit test for the above path by mocking the REST object as SomeObject wont be initialized for the test.

Below is the test code,

@Before
    public void setUp(){
        rest = mock(REST.class);
        SomeObject someObj = new SomeObject("String1","String2");
        List<SomeObject> someList = new ArrayList<>();
        someList.add(someObj);

        clientResponse = Response.ok(someList).build();

        when(rest.getSomething()).thenReturn(clientResponse);
    }

@Test
public void getSomethingTest() throws Exception {

    Response get = target("/somepath").request().get();
        
    assertThat(get.getStatus(), is(200));

}

Problem is I am facing NULLPointerException where I do get response from the target.

java.lang.NullPointerException
        at org.glassfish.jersey.test.JerseyTest.target(JerseyTest.java:566)
        at org.glassfish.jersey.test.JerseyTest.target(JerseyTest.java:580)
        at io.RESTTest.getSomething(RESTTest.java:72)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        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.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
        at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
        at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
        at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
        at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
        at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Can someone please suggest me a way to het the response from the REST API or tell me why there is a NULLPointerException

Aucun commentaire:

Enregistrer un commentaire