2017-06-22 110 views
1

在基於彈簧啓動的Rest項目上工作我有一個像這樣的控制器,它調用服務和服務層調用dao層。現在我正在爲控制器編寫單元測試代碼。當我運行這個錯誤說彈簧啓動單元測試斷言錯誤

java.lang.AssertionError: expected:<201> but was:<415>

我不知道我做錯了:

public class CustomerController { 
     private static final Logger LOGGER = LogManager.getLogger(CustomerController.class); 
     @Autowired 
     private CustomerServices customerServices; 
     @Autowired 
     private Messages MESSAGES; 
     @Autowired 
     private LMSAuthenticationService authServices; 
     @RequestMapping(value = "/CreateCustomer", method = RequestMethod.POST) 
     public Status createCustomer(@RequestBody @Valid Customer customer, BindingResult bindingResult) { 
      LOGGER.info("createCustomer call is initiated"); 
      if (bindingResult.hasErrors()) { 
       throw new BusinessException(bindingResult); 
      } 
      Status status = new Status(); 
      try { 
       int rows = customerServices.create(customer); 
       if (rows > 0) { 
        status.setCode(ErrorCodeConstant.ERROR_CODE_SUCCESS); 
        status.setMessage(MESSAGES.CUSTOMER_CREATED_SUCCESSFULLY); 
       } else { 
        status.setCode(ErrorCodeConstant.ERROR_CODE_FAILED); 
        status.setMessage(MESSAGES.CUSTOMER_CREATION_FAILED); 
       } 
      } catch (Exception e) { 
       LOGGER.info("Cannot Create the Customer:", e); 
       status.setCode(ErrorCodeConstant.ERROR_CODE_FAILED); 
       status.setMessage(MESSAGES.CUSTOMER_CREATION_FAILED); 
      } 
      return status; 
     } 
       } 

測試的CustomerController

public class CustomerControllerTest extends ApplicationTest { 

     private static final Logger LOGGER = LogManager.getLogger(CustomerControllerTest.class); 


     @Autowired 
     private WebApplicationContext webApplicationContext; 

     private MockMvc mockMvc; 

     @MockBean 
     private CustomerController customerController; 

     @Before 
     public void setup() { 
      this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); 
     } 

     Status status = new Status(200,"customer created successfully","success"); 

     String customer = "{\"customerFullName\":\"trial8900\",\"customerPhoneNumber\": \"trial8900\", \"customerEmailID\": \"[email protected]\",\"alternateNumber\": \"trial8900\",\"city\": \"trial8900\",\"address\":\"hsr\"}"; 

     @Test 
     public void testCreateCustomer() throws Exception { 

      String URL = "http://localhost:8080/lms/customer/CreateCustomer"; 
      Mockito.when(customerController.createCustomer(Mockito.any(Customer.class),(BindingResult) Mockito.any(Object.class))).thenReturn(status); 
      // execute 
      MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post(URL) 
        .contentType(MediaType.APPLICATION_JSON_UTF8) 
        .accept(MediaType.APPLICATION_JSON_UTF8) 
        .content(TestUtils.convertObjectToJsonBytes(customer))).andReturn(); 
      LOGGER.info(TestUtils.convertObjectToJsonBytes(customer)); 

      // verify 
      MockHttpServletResponse response = result.getResponse(); 
      LOGGER.info(response); 
      int status = result.getResponse().getStatus(); 

      LOGGER.info(status); 

      assertEquals(HttpStatus.CREATED.value(), status); 
     } 

    } 
+0

你的測試沒有太多測試。你應該模擬'UserService'而不是你的控制器。除此之外,您正嘗試將JSON字符串轉換爲JSON字符串。所以不知道你爲什麼要這麼做。創建一個'Customer'並將其轉換爲JSON或按原樣發佈JSON。 –

回答

0

HTTP狀態415是「不支持的介質類型」。您的端點應該標記爲@Consumes(也可能是@Produces)註釋,指定它從客戶端期望什麼類型的媒體類型,以及它返回給客戶端的媒體類型。

由於我看到您的測試代碼使用MediaType.APPLICATION_JSON_UTF8來執行您的生產代碼,因此您應該將您的端點標記爲消耗並生成APPLICATION_JSON_UTF8。

然後,您還需要確保在錯誤處理過程中沒有任何可怕的錯誤發生,因爲在捕獲生產代碼生成的異常並生成HTTP響應的過程中,錯誤處理代碼可能會生成一些東西不同,例如使用包含HTML格式的錯誤消息的有效內容生成錯誤狀態響應,該錯誤消息將具有內容類型爲「text/html」的內容,而您的測試代碼無法理解哪個內容類型需要json。