gpt4 book ai didi

java - Spring Boot单元测试断言错误

转载 作者:太空宇宙 更新时间:2023-11-04 11:23:06 24 4
gpt4 key购买 nike

在基于 Spring Boot 的 Rest 项目上工作我有一个像这样的 Controller 其中调用service,service层调用dao层。现在我正在为 Controller 编写单元测试代码。当我运行这个错误时显示

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\": \"trial8900@g.com\",\"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);
}

}

最佳答案

HTTP 状态 415 是“不支持的媒体类型”。您的端点应使用 @Consumes (也可能是 @Produces)注释进行标记,指定它期望从客户端获得什么类型的媒体类型,以及它返回给客户端的媒体类型类型。

由于我看到您的测试代码使用 MediaType.APPLICATION_JSON_UTF8 来执行您的生产代码,因此您可能应该将您的端点标记为使用和生成 APPLICATION_JSON_UTF8。

然后你还需要确保你的错误处理中没有发生任何严重错误,因为在捕获生产代码生成的异常并生成 HTTP 响应的过程中,你的错误处理代码可能会生成不同的东西,例如生成错误状态响应,其负载包含 HTML 格式的错误消息,其内容类型为“text/html”,需要 json 的测试代码无法理解该消息。

关于java - Spring Boot单元测试断言错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44692122/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com