使用Jersey Test Framework进行REST API测试

时间:2016-06-26 16:57:56

标签: rest unit-testing jersey mockito jersey-test-framework

REST.class中定义的REST API如下所示,



@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();
}
&#13;
&#13;
&#13;

我试图通过模拟REST对象来运行上述路径的单元测试,因为SomeObject不会为测试初始化​​。

以下是测试代码,

&#13;
&#13;
@RunWith(MockitoJUnitRunner.class)
public class RESTTest extends JerseyTest{

    
    @Mock
    private SomeObject someObj;
    
    @Mock
    private REST rest;
    private Response clientResponse;

    @Override
    protected Application configure(){
        ResourceConfig config = new ResourceConfig(REST.class);
        return config;
    }


@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));

}
}
&#13;
&#13;
&#13;

问题是我面临NULLPointerException,我从目标中获得响应。

&#13;
&#13;
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)
&#13;
&#13;
&#13;

有人可以建议我从REST API获取响应的方法或告诉我为什么会有NULLPointerException

0 个答案:

没有答案