Ich versuche, Tests für meine mvc-Controller zu tun, damit ich weiß, dass sie richtig funktionieren. Ich werde nach dem gesamten Code und dann erklären, was passiert ist:jUnit mit Spring MVC gibt eine 400 statt eine 200
mein Controller
@RequestMapping(value ="Residents/create", method=RequestMethod.POST)
public ResponseEntity<Object> createNewResident(@RequestBody Resident
resident){
Apartment apartment = apartmentService.findByApartmentId(167);
resident.setApartment(apartment);
System.out.println("slack api");
String requestUrl = "SlackStringThatIChanged" +"&email=" +resident.getEmail() +
"&first_name=" + resident.getFirstName() + "&last_name=" + resident.getLastName();
try {
URL url = new URL(requestUrl);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("GET");
//View Slack response
/*BufferedReader br = new BufferedReader(new InputStreamReader(httpCon.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
System.out.println(line);
}
br.close();
*/
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ResponseEntity.ok(residentService.createResident(resident));
}
Mein Prüfnorm
@Test//this still has problems with the apartment...
public void createNewResidentTest() throws Exception {
apartmentComplex = new ApartmentComplex();
apartmentComplex.setComplexId(1);
apartmentComplex.setAddress("1234 theAddress");
apartmentComplex.setEmail("[email protected]");
apartmentComplex.setName("westerly");
apartmentComplex.setPhone("8548");
apartmentComplex.setWebsite("www.serwtet.com");
apartmentComplex.setPhoto("weewtfsw");
apartment = new Apartment();
apartment.setApartmentId(1);
apartment.setApartmentNumber(101);
apartment.setCapacity(6);
apartment.setOccupancy(3);
List<Apartment> apt = new ArrayList<>();
apt.add(apartment);
apartmentComplex.setApartments(apt);
resident = new Resident();
resident.setResidentId(1);
resident.setFirstName("Cool");
resident.setLastName("Man");
resident.setEmail("[email protected]");
resident.setPhone("548791234");
resident.setSlackId("32f4443rwd");
resident.setAbout("Yeah boi");
resident.setApartment(apartment);
Set<Resident> resLis = new HashSet<Resident>();
resLis.add(resident);
apartment.setResidents(resLis);
mvc.perform(MockMvcRequestBuilders.post("http://localhost:8181/api/Residents/create", resident)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
Also, ich versuche, dass die Steuerung zu testen, die einen Bewohner schafft . Ich bekam zuerst einen Fehler, der etwas wie "Referenz ist Nullpointerexception" sagte. Ich fand heraus, dass der Bewohner in einer Wohnung wohnte und eine Wohnung in der Apartmentanlage existierte. Nach der Fixierung dieser Fehler mich auf stolpern „Status ausgenommen < 200> aber war < 400>“ und in der Ausgabe der Konsole bekomme ich diese:
MockHttpServletRequest:
HTTP Method = POST
Request URI = /api/Residents/create
Parameters = {}
Headers = {Accept=[application/json]}
Handler:
Type = com.application.controller.ResidentController
Method = public org.springframework.http.ResponseEntity<java.lang.Object> com.application.controller.ResidentController.createNewResident(com.application.model.Resident)
Async:
Async started = false
Async result = null
Resolved Exception:
Type = org.springframework.http.converter.HttpMessageNotReadableException
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
Ich weiß nicht wirklich, was passiert ist oder was das Problem sein könnte . Vielleicht implementiere ich den Test falsch?
Bitte lassen Sie mich wissen, was Sie denken! Vielen Dank.
Ist Ihre Controller-Klasse mit RequestMapping (value = "api") annotiert? –
Per MockMvcRequestBuilders, zweiter Parameter von post ist uriVars - null oder mehr URI-Variablen –
Seine werfen org.springframework.http.converter.HttpMessageNotReadableException, Haben Sie Jackson API-Bibliothek in Ihren Abhängigkeiten und ist Resident-Klasse-Eigenschaft mit JSON-Annotation kommentiert. Mit Fiddler überprüfen Sie, was in den Körper Ihrer Anfrage geht, es scheint Körper ist nicht das gleiche, was Ihr Controller erwartet. –