[英]jUnit with Spring MVC returns a 400 instead a 200
我正在嘗試為我的MVC控制器進行測試,因此我知道它們工作正常。 我將首先發布所有代碼,然后解釋發生了什么:
我的控制器
@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) );
}
我的測試方法
@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("some@email.com");
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("coolman@cool.com");
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());
}
因此,我正在嘗試測試創建居民的那個控制器。 我首先遇到一個錯誤,說“引用為nullpointerexception”之類的東西,我發現這是由於居民住在公寓中,而公寓大樓中卻有公寓。 修復此錯誤后,我偶然發現“狀態除<200>以外為<400>”,並且在控制台輸出中得到以下信息:
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
我真的不知道發生了什么或可能是什么問題。 也許我執行的測試有誤?
請讓我知道您有任何想法! 謝謝。
如下所示呢?
@Test//this still has problems with the apartment...
public void createNewResidentTest() throws Exception {
Resident resident = new Resident();
// build resident
// convert to json string
mvc.perform(MockMvcRequestBuilders.post("/api/Residents/create")
.contentType(MediaType.APPLICATION_JSON)
.content(json)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.