集成测试期间@Context为null

时间:2016-04-05 21:10:18

标签: java spring spring-boot integration-testing rest-assured

我在休息应用程序上使用spring boot 1.3.0。我正在尝试使用RestAssured创建集成测试。我在@Context上遇到了问题。它没有被注入控制器以便我获取URI。

这是测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebIntegrationTest("server.port=8080")
@ActiveProfiles(value = {"testing"})
public class InvoiceRestServiceIT {

    private static String INVOICE_DATA = "<invoice data>";
    private static String INVOICE_URL = "/customers/1q2w3e4r/invoices";

    @Test
    public void testPostInvoice() {
        given()
           .contentType(ContentType.JSON)
           .accept(ContentType.JSON)
           .body(INVOICE_DATA)
         .expect()
            .statusCode(200)
            .body(containsString("entity"), containsString("id"))
         .when()
            .post(INVOICE_URL);;
    }
}

这是服务类(InvoiceRestService.java):

@Autowired
private InvoiceController invoiceController;

@POST
public EntityResponse add(InvoiceResource invoiceResource) {
    return new EntityResponse(invoiceController.createInvoice(invoiceResource));
}

这里是控制器(扩展RestController的InvoiceController):

protected Customer getCustomer() {
    Customer customer = customerRepository.findOne(getCustomerId());
...
}

这是发生错误的地方(RestController.java):

@Context
private HttpServletRequest request;

protected String getCustomerStringId() {
    Matcher matcher = pattern.matcher(request.getRequestURI()); //<---- Here, request object is null
    if (!matcher.find()) {
        throw new IllegalStateException("Cannot find customer id in request URL");
    }

    return matcher.group(1);
}

这里是错误日志:

java.lang.NullPointerException: null
at org.store.rest.RestController.getCustomer(RestController.java:90)
at org.store.rest.invoice.InvoiceController.createInvoice(InvoiceController.java:87)
at org.store.rest.invoice.InvoiceController$$FastClassBySpringCGLIB$$2d6c1a2e.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
at com.store.rest.invoice.InvoiceController$$EnhancerBySpringCGLIB$$90cabdc3.createInvoice(<generated>)
at com.store.rest.invoice.InvoiceRestService.add(InvoiceRestService.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:497

有没有人遇到过这个?如何解决这个问题?

3 个答案:

答案 0 :(得分:3)

您的控制器似乎更像是实用程序类,而您的服务实际上是一个控制器。因此,在这方面,您的InvoiceController可能不是JAX RS根资源。

我认为最简单的解决方案(如果您想沿着此路线继续)是删除@Context private HttpServletRequest request;中的RestController声明并将其移至InvoiceRestService,如下所示:

@POST
public EntityResponse add(InvoiceResource invoiceResource, @Context HttpServletRequest request) {
    return new EntityResponse(invoiceController.createInvoice(invoiceResource, request));
}

然后,您需要将request传递给InvoiceController实用程序。

但是......最优雅的解决方案就是让JAX RS解析传入路径中的ID。使用@Path,您可以为路径的命名段定义占位符,并将这些段注入到JAX RS端点方法中,如下所示:

@Path("/customers/{customerId}/invoices")
public class InvoiceRestService {

  @POST
  public EntityResponse add(@PathParam("customerId") String customerId, InvoiceResource invoiceResource) {
    return new EntityResponse(invoiceController.createInvoice(invoiceResource, customerId));
  }
}

答案 1 :(得分:0)

从REST Assured 2.2开始,您可以使用RestAssuredMockMvc模块中的spring-mock-mvc,并避免启动Web服务器来进行测试。

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = Application.class, 
    loader = AnnotationConfigWebContextLoader.class)
public class ApplicationTest {
    @Autowired
    private WebApplicationContext wac;

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

    @Test
    public void test() {
        // your REST Assured test for any controller
    }
}

答案 2 :(得分:0)

使用@Autowired注释进行请求

@Autowired
private HttpServletRequest request;