2016-02-09 14 views
5

Ho la seguente struttura nel progetto Spring MVC che ho creato.java.lang.IllegalArgumentException: Tipo non riconosciuto: [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; 
    } 
} 
} 

La classe Controller è la seguente:

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

} 

ottengo il seguente messaggio di errore dopo l'esecuzione della classe CustomerTest

CustomerTest 

test.java.CustomerTest getExistingCustomerEntry (test.java .CustomerTest) org.springframework.web.util.NestedServletException: elaborazione richiesta non riuscita; eccezione annidata è java.lang.IllegalArgumentException: riconosciuto Tipo: [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) 

causato da: java.lang.IllegalArgumentException: Tipo non riconosciuto: [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 

ho il debug test JUnit e la classe controller e v'è un'eccezione argomento null sul tipo di ritorno della risorsa sulla riga seguente

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

che cosa sto facendo male qui?

risposta

14

Ho subito qualcosa di simile ieri. Se stai usando jackson 2.7 - con QUALSIASI versione di primavera meno della primavera 4.3 - probabilmente otterrai questo.

Quindi downgrade a 2.6.5 (a seconda della versione di primavera) - e c'è un buon cambiamento funzionerà.

+0

Ha lavorato grazie :) – MindBrain

+0

ha lavorato per me; Grazie! –

+0

HHmm Ho appena affrontato questo problema. Quello che ho fatto, ho usato 'jackson-core',' jackson-annotations', 'jackson-datatype-jsr310' alla versione' 2.7.1'. 'jackson-databind' alla versione' 2.7.1-1'. 'jackson-datatype-hibernate5' alla versione' 2.6.5'. Le cose hanno iniziato a funzionare. Sto usando la versione di primavera '4.2.4.RELEASE' e' hibernate-entitumanger' '5.1.0.Final'. Per me il colpevole era 'jackson-datatype-hibernate5'. il downgrade a '2.6.5' era una soluzione per me. Grazie – Basit

Problemi correlati