Ich verwende Springboot 1.3.0 auf einer Rest-Anwendung. Ich versuche einen Integrationstest mit RestAssured zu erstellen. Ich habe ein Problem mit dem @Context. Es wird nicht in den Controller injiziert, damit ich den URI bekomme.@Context ist Null während des Integrationstests
Hier ist die Testklasse:
@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);;
}
}
Hier ist die Service-Klasse (InvoiceRestService.java):
@Autowired
private InvoiceController invoiceController;
@POST
public EntityResponse add(InvoiceResource invoiceResource) {
return new EntityResponse(invoiceController.createInvoice(invoiceResource));
}
Hier ist die Steuerung (InvoiceController RestController erstreckt):
protected Customer getCustomer() {
Customer customer = customerRepository.findOne(getCustomerId());
...
}
Hier ist, wo der Fehler auftritt (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);
}
Hier ist das Fehlerprotokoll:
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
Hat das jemand begegnet? Wie kann ich dieses Problem lösen?
Feld Injektion verursacht alle möglichen Probleme. Das Übergeben der Anforderung als Methodenparameter ist einfacher und zuverlässiger. – chrylis
Ich habe das getan, bevor ich diese Frage gepostet habe. – Leo