2017-06-22 74 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。