ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-集成测试期间@Context为null

2019-11-18 21:19:26  阅读:251  来源: 互联网

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


我在其余应用程序上使用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));
}

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

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

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

解决方法:

看来您的控制器更像是实用程序类,并且您的服务实际上是一个控制器.因此,就此而言,您的InvoiceController可能不是JAX RS根资源.

我认为最简单的解决方案(如果要继续执行此方法)是删除@Context私有HttpServletRequest请求;在RestController中声明,然后将其移动到InvoiceRestService,如下所示:

@POST
public EntityResponse add(InvoiceResource invoiceResource, @Context HttpServletRequest request) {
    return new EntityResponse(invoiceController.createInvoice(invoiceResource, 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));
  }
}

标签:spring-boot,integration-testing,rest-assured,spring,java
来源: https://codeday.me/bug/20191118/2031251.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有