java-集成测试期间@Context为null
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-集成测试期间@Context为null,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4052字,纯文字阅读大概需要6分钟。
内容图文
![java-集成测试期间@Context为null](/upload/InfoBanner/zyjiaocheng/665/b036a8f1d07f4583a9e9f055c1f2ec35.jpg)
我在其余应用程序上使用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));
}
}
内容总结
以上是互联网集市为您收集整理的java-集成测试期间@Context为null全部内容,希望文章能够帮你解决java-集成测试期间@Context为null所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。