5
我在創建的Spring MVC項目中擁有以下結構。java.lang.IllegalArgumentException:無法識別類型:[null]
public class CustomerTest {
//@Mock
//private Customer customer;
@Mock
private CustomerService service;
@InjectMocks
private CustomersController custcontroller;
private MockMvc mockmvc;
/* Before executes before each and every test */
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mockmvc = MockMvcBuilders.standaloneSetup(custcontroller).build();
}
/*test that our requests are being sent*/
@Test
public void getExistingCustomerEntry() throws Exception {
// assertEquals(expected parameter,your parameter)
Customer cust = new Customer();
cust.setCustomer_id(1L);
cust.setName("sam");
when(service.find(1L)).thenReturn(cust);
try {
mockmvc.perform(get("/rest/cust-entries/1"))
.andExpect((jsonPath("$.name", Is.is(cust.getName()))))
.andExpect(jsonPath("$.links[*].href", hasItem(endsWith("/cust-entries/1"))))
.andExpect(status().isOk())
.andDo(print());
} catch(Exception e) {
throw e;
}
}
}
控制器類是如下:
@Controller
public class CustomersController {
private CustomerService service;
public CustomersController(CustomerService service) {
this.service = service;
}
/*used a path variable to allow to bind a variable in the url to the Java variable*/
@RequestMapping(value = "/rest/cust-entries/{customer_id}", method= RequestMethod.GET)
public ResponseEntity<CustomerResource> getCustomerEntry(@PathVariable Long customer_id) {
Customer custEntry = service.find(customer_id);
System.out.println("Was here" + customer_id);
CustomerResource resource = new CustomerServiceEntryAsm().toResource(custEntry);
return new ResponseEntity<CustomerResource>(resource,HttpStatus.OK);
}
}
我運行CustomerTest類
CustomerTest
test.java.CustomerTest getExistingCustomerEntry(test.java後得到下面的錯誤信息.CustomerTest) org.springframework.web.util.NestedServletException:請求處理失敗;嵌套異常是java.lang.IllegalArgumentException異常:無法識別的類型:空]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:981)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:860)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:65)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:167)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:155)
at test.java.CustomerTest.getExistingCustomerEntry(CustomerTest.java:74)
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)
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.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
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.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
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:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
所致:java.lang.IllegalArgumentException異常:無法識別類型:空]
at com.fasterxml.jackson.databind.type.TypeFactory._fromAny(TypeFactory.java:1109)
at com.fasterxml.jackson.databind.type.TypeFactory.constructType(TypeFactory.java:566)
at com.fasterxml.jackson.databind.type.TypeFactory.constructType(TypeFactory.java:602)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.getJavaType(AbstractJackson2HttpMessageConverter.java:311)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:249)
at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:100)
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:222)
at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:183)
at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:80)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:126)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
... 33 more
我調試JUnit測試和控制器類和在下面的行上資源的返回類型有一個空參數異常
return new ResponseEntity<CustomerResource>(resource,HttpStatus.OK);
我在做什麼錯在這裏?
工作感謝:) – MindBrain
爲我工作;謝謝! –
嗯,我剛剛面對這個問題。我做了什麼,我使用了'jackson-core','jackson-annotations','jackson-datatype-jsr310'到版本'2.7.1'。 'jackson-databind'到版本'2.7.1-1'。 'jackson-datatype-hibernate5'到版本'2.6.5'。事情開始工作。我使用的是春季版本'4.2.4.RELEASE'和'hibernate-entitumanger'版本'5.1.0.Final'。對我來說罪魁禍首是'jackson-datatype-hibernate5'。將這個降級到'2.6.5'就是我的解決方案。謝謝 – Basit