RestAssuredMockMvc - 请求处理失败;嵌套异常是java.lang.NullPointerException

时间:2018-03-14 12:14:09

标签: unit-testing spring-boot mocking rest-assured

我正在尝试使用RestAssured模拟一些用于集成测试的Web服务和控制器。我能够使用实时容器运行测试,即从eclipse启动spring boot并在http://localhost:8080/ws/testapi上运行测试

但是我无法使用RestAssuredMockMvc来嘲笑它,而是说

`org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:65)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:160)
    at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:127)
    at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:155)
    at io.restassured.module.mockmvc.internal.MockMvcRequestSenderImpl.performRequest(MockMvcRequestSenderImpl.java:195)
    at io.restassured.module.mockmvc.internal.MockMvcRequestSenderImpl.sendRequest(MockMvcRequestSenderImpl.java:431)
    at io.restassured.module.mockmvc.internal.MockMvcRequestSenderImpl.get(MockMvcRequestSenderImpl.java:609)
    at io.restassured.module.mockmvc.internal.MockMvcRequestSenderImpl.get(MockMvcRequestSenderImpl.java:76)
    at com.ashland.vioc.pos.webservice.webmvc.controller.CarFaxProxyControllerIntegrationTest_RAMock.testGetServicesByVin_success_aroundBody2(CarFaxProxyControllerIntegrationTest_RAMock.java:74)
    at com.ashland.vioc.pos.webservice.webmvc.controller.CarFaxProxyControllerIntegrationTest_RAMock$AjcClosure3.run(CarFaxProxyControllerIntegrationTest_RAMock.java:1)
    at org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$around$org_springframework_transaction_aspectj_AbstractTransactionAspect$1$2a73e96cproceed(AbstractTransactionAspect.aj:66)
    at org.springframework.transaction.aspectj.AbstractTransactionAspect$AbstractTransactionAspect$1.proceedWithInvocation(AbstractTransactionAspect.aj:72)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
    at org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$around$org_springframework_transaction_aspectj_AbstractTransactionAspect$1$2a73e96c(AbstractTransactionAspect.aj:70)
    at com.ashland.vioc.pos.webservice.webmvc.controller.CarFaxProxyControllerIntegrationTest_RAMock.testGetServicesByVin_success(CarFaxProxyControllerIntegrationTest_RAMock.java:67)
    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.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.NullPointerException
    at com.ashland.vioc.pos.webservice.webmvc.controller.CarFaxProxyController.getServicesByVin(CarFaxProxyController.java:40)
    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.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    ... 49 more

`

我是Spring新手,只参与单元测试应用程序。不确定我们是否需要进行一些配置更改才能进行模拟。我提到https://github.com/rest-assured/rest-assured/wiki/Usage#spring-mock-mvc-module  但无法找到太多信息或可能是我遗失了一些东西。

package com.hl.pos.webservice.controller;

import static com.ashland.vioc.pos.webservice.carfax.CarFaxServiceImplIntegrationTest.*;
import static org.hamcrest.Matchers.*;
import static io.restassured.module.mockmvc.RestAssuredMockMvc.*;   
import javax.inject.Inject;    
import org.junit.Before;
import org.junit.Test;
import com.ashland.vioc.pos.webservice.master.domain.StoreDataOnDemand;

@Category(MockControllerIntegrationTest.class)
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles(Profiles.TESTING_MOCK_MVC)
@Transactional
@AutoConfigureMockMvc
public class CarControllerIntegrationTest {

    private static final String REQUEST_MAPPING = "/ws/v1/car";
    @Autowired
    protected WebApplicationContext wac;

    @Before
    public void setUp(){
       RestAssuredMockMvc.webAppContextSetup(wac);
       RestAssuredMockMvc.standaloneSetup(new CarController());
    }

    @Test
    public void testGetServices() throws Exception {
        final String cnum= VALID_CNUM;
        final String store = "1234";
        given().
                standaloneSetup(new CarController()).
                param("storeNum",storeNumber).
       // given().
                //param("store",store).
        when().
            get(REQUEST_MAPPING + "/v/{cnum}/carHistory",cnum).
        then().
            assertThat().statusCode(200).
            body("carDetails[0].serviceDate",notNullValue());
    }

}

相同的测试在具有相同配置的同一文件中与Mockmvc运行良好,问题仅在于RestAssured。

@Test
        public void testGetServices() throws Exception {
            final String cnum= VALID_CNUM;
            final String store = "1234";
            mockMvc.perform(get(REQUEST_MAPPING + "/v/{cnum}/carHistory", cnum).with(params().storeNum(storeNumber)))
                .andExpect(200);
        }

1 个答案:

答案 0 :(得分:0)

最后,我得到了它的工作。需要使用import static import static io.restassured.module.mockmvc.RestAssuredMockMvc.*;

然后,我们不必使用RestAssuredMockMvc并且不需要控制器。将从以下代码中清楚 -

package com.hl.pos.webservice.controller;

import static com.ashland.vioc.pos.webservice.carfax.CarFaxServiceImplIntegrationTest.*;
import static org.hamcrest.Matchers.*;
import static io.restassured.module.mockmvc.RestAssuredMockMvc.*;   
import javax.inject.Inject;    
import org.junit.Before;
import org.junit.Test;
import com.ashland.vioc.pos.webservice.master.domain.StoreDataOnDemand;

@Category(MockControllerIntegrationTest.class)
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles(Profiles.TESTING_MOCK_MVC)
@Transactional
@AutoConfigureMockMvc
public class CarControllerIntegrationTest {

    private static final String REQUEST_MAPPING = "/ws/v1/car";
    @Autowired
    protected WebApplicationContext wac;

    @Before
    public void setUp(){
       webAppContextSetup(wac);
    }

    @Test
    public void testGetServices() throws Exception {
        final String cnum= VALID_CNUM;
        final String store = "1234";
        given().
                param("storeNum",storeNumber).
        when().
            get(REQUEST_MAPPING + "/v/{cnum}/carHistory",cnum).
        then().
            assertThat().statusCode(200).
            body("carDetails[0].serviceDate",notNullValue());
    }

}

我们删除了RestAssuredMockMvc.standaloneSetup(new CarController());,这是一个抛出异常。需要使用webAppContextSetup