2016-05-18 4 views
0

Ich habe Klasse Type1 wie folgt aus:vererbten Klasse Gang in @RequestMapping mit MockMVC

public class Type1 extends TypeParent {} 

Ich habe eine Methode Post in meinem Controller Hexe Ich werde mit MockMvc testen:

@RequestMapping(value = "/actions", method = RequestMethod.POST) 
public Map<String, Name> gettypes(@Valid @RequestBody Type1 request) { ... } 

Aber ich verstehe nicht, warum der folgende Testcode nicht funktioniert.

Type1 type= new Type1(); 

mockMvc.perform(post("/actions").contentType(JsonUtils.APPLICATION_JSON_UTF8).content(JsonUtils.toJson(type))) 
    .andExpect(status().isOk()) 
    .andDo(print()); 

Ausgang:

java.lang.AssertionError: Status expected:<200> but was:<400>

Sonst mit: TypeParent typeParent = new TypeParent(); ist es Arbeit sehr fein:

mockMvc.perform(post("/actions").contentType(JsonUtils.APPLICATION_JSON_UTF8) 
    .content(JsonUtils.toJson(typeParent))) 
    .andExpect(status().isOk()) 
    .andDo(print()); 

Ich verstand, dass eine geerbte Objekt vorbei, nicht funktioniert, wie kann ich tun Löse dieses Problem?

+0

Können Sie bitte versuchen Sie dies: 'mockMvc.perform (post ("/ Aktionen") content (JsonUtils.APPLICATION_JSON_UTF8) .content (JsonUtils.toJson (Typ)).) .andDo (print()) .andExpect (status(). isOk()); ' und setzen Sie die Fehlerausgabe hier? – mtyurt

+0

Gelöst Ausnahme: Type = org.springframework.http.converter.HttpMessageNotReadableException MockHttpServletResponse: Status = 400 – aryan

+0

denke ich, das Problem in (@RequestBody Typ1 Anfrage), weil 'Typ' eine andere Klasse geerbt wird, sollte es Arbeit sein. aber ich weiß nicht, wie ich es lösen soll? – aryan

Antwort

0

ich dieses Problem lösen ... es ist komisch:

In der Klasse Typ1 Ich habe eine Funktion: isEmpty() {..}

Wenn ich es konvertiere ich diese erhalten JSon: { "leer": true, }

Die Lösung, dass ich den Namen dieser Funktion zu myFctEmpty() {..} ändern !!!!