java.lang.IllegalArgumentException:无法识别的类型:[null]

时间:2016-02-09 12:35:00

标签: spring-mvc mockito junit4

我在我创建的Spring MVC项目中有以下结构。

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:无法识别的类型:[null]

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:无法识别的类型:[null]

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);

我在这里做错了什么?

1 个答案:

答案 0 :(得分:14)

昨天我经历了这样的事情。如果您使用的是杰克逊2.7 - 任何弹簧版本低于弹簧4.3 - 您可能会得到这个。

所以将杰克逊降级到2.6.5(取决于你的春季版本) - 并且它有一个很好的改变它将起作用。