Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalArgumentException: Unrecognized Type: [null]

I have the following structure in the Spring MVC project I have created.

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

The Controller Class is as below :

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

}

I get the following error message after running the CustomerTest class

CustomerTest

test.java.CustomerTest getExistingCustomerEntry(test.java.CustomerTest) org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: Unrecognized Type: [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)

Caused by: java.lang.IllegalArgumentException: Unrecognized Type: [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

I debugged the junit test and the controller class and there is a null argument exception on the return type of the resource on the following line

return new ResponseEntity<CustomerResource>(resource,HttpStatus.OK);

What am I doing wrong here?

like image 677
MindBrain Avatar asked Feb 09 '16 12:02

MindBrain


1 Answers

I suffered through something like this yesterday. If you are using jackson 2.7 - with ANY version of spring less than spring 4.3 - you'll probably get this.

So downgrade jackson to 2.6.5 (depending on your spring version) - and there is a good change it'll work.

like image 168
Tim Funk Avatar answered Sep 27 '22 23:09

Tim Funk