简体   繁体   English

Junit 测试用例 - 错误 - 预期状态:<200> 但为:<500>

[英]Junit testcase - error - status expected:<200> but was:<500>

Am running Junit test cases for my project module.正在为我的项目模块运行 Junit 测试用例。 After the execution of the test case am getting the below error report.执行测试用例后,我收到以下错误报告。

ClaimControllerTest.shouldReturnClaimWithValidationResult:417 Status expected:<200> but was:<500>

Am using the below maven comment to execute the test case for a specific module.我使用下面的 maven 注释来执行特定模块的测试用例。

mvn clean compile verify -Ptest

But, my project have many module dependency.但是,我的项目有很多模块依赖。 How can i execute my test case for my complete project ( will all modules )我如何为我的完整项目执行我的测试用例(所有模块)

The above error is pointing to the below piece of test case code上面的错误指向下面的测试用例代码

 mockMvc.perform(get(String.format("/claims/%d/validation-result", claim.getId())))
  .andExpect(status().isOk())
  .andExpect(jsonPath("$.catClaimNumber").value(claim.getCatClaimNumber().intValue()))
  .andExpect(jsonPath("$.validationResult.programErrorCode").value("UNTHRZD_EXTRNL_USR"))
  .andExpect(jsonPath("$.validationResult.hasErrors").value(true));

Update 1 :更新 1:

Am seeing the below null pointer exception after doing Sys out在执行 Sys 后看到下面的空指针异常

2019-01-03 15:46:27,401 [main] ERROR api.controller.BaseController: 83 - Unhandled exception while processing request for URL : http://localhost/claims/1901/validation-result with exception : null java.lang.NullPointerException

For the below code :对于以下代码:

mockMvc.perform(get(String.format("/claims/%d/validation-result", claim.getId()))).andDo(print());

It is telling you that instead of a success code 200 from calling your rest endpoint, it received an error code of 500. It means that your rest end point threw an exception.它告诉您,调用您的休息端点时收到的错误代码不是 200,而是 500。这意味着您的休息端点抛出了异常。

you may want to print the full response with exception to further debug您可能想要打印完整的响应,但要进一步调试

mockMvc.perform(get(String.format("/claims/%d/validation-result", claim.getId()))).andDo(print());

You have two questions:你有两个问题:

  1. Getting 500 instead of 200 - This is already answered by @mkjh获得 500 而不是 200 - @mkjh 已经回答了这个问题
  2. How to execute test case for complete project ( will all modules ) - For this you should try mockito (mocking other modules) because you cannot and should not take into account of all modules until and unless it's an integration test where in you make sure all the modules are up and running or else you will again end up getting 500 error which basically tells that there is some internal server error happened and in most of the cases it happens because of some services are not up如何执行完整项目的测试用例(将所有模块) - 为此,您应该尝试 mockito(模拟其他模块),因为您不能也不应该考虑所有模块,除非它是一个集成测试,您确保所有模块已启动并正在运行,否则您最终将再次收到 500 错误,这基本上表明发生了一些内部服务器错误,并且在大多数情况下是由于某些服务未启动而发生的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM