Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何对自定义异常执行JUnit测试_Java - Fatal编程技术网

Java 如何对自定义异常执行JUnit测试

Java 如何对自定义异常执行JUnit测试,java,Java,我有一个自定义异常,用于处理程序的预期错误,下面是代码 @ControllerAdvice @RestController public class DashboardException { @ExceptionHandler({Exception.class, IOException.class, ParseException.class, JsonProcessingException.class}) public final ResponseEntity<ErrorDetails&

我有一个自定义异常,用于处理程序的预期错误,下面是代码

@ControllerAdvice
@RestController
public class DashboardException {

@ExceptionHandler({Exception.class, IOException.class, ParseException.class, JsonProcessingException.class})
public final ResponseEntity<ErrorDetails> dataNotFoundException(Exception ex, WebRequest request) {
    ErrorDetails errorDetails = new ErrorDetails();
    errorDetails.setTimestamp(new Date().toString());
    errorDetails.setMessage(ex.getMessage());
    errorDetails.setPath(request.getDescription(false));
    errorDetails.setStatus(HttpStatus.BAD_REQUEST.value());
    errorDetails.setError(HttpStatus.BAD_REQUEST);
    return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST);
}
   }

我对自定义异常只有一点了解。我错过了什么?谢谢您的帮助。

我找到了如何覆盖自定义异常的方法。我刚刚在我的控制器上加入了一个测试,该测试将使端点失败,它确实捕获了一个异常,并涵盖了我的自定义异常。

您可能想看看您缺少什么,这里是对您不了解的内容的清晰详细描述。“它不工作”永远不是一个合适的错误描述。
 @RunWith(SpringJUnit4ClassRunner.class)

@WebAppConfiguration

@ContextConfiguration(classes = { DashboardException.class, TestConfiguration.class, DataController.class })
public class testDashboardException {

    private MockMvc mockMvc;

    @Autowired
    WebApplicationContext wac;

    @Before
    public void setUp() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

    @Configuration
      @EnableWebMvc
      public static class TestConfiguration { }

     @Controller
      @RequestMapping("/tests")
      public static class RestProcessingExceptionThrowingController {


        @GetMapping(value = "/exception")
        public @ResponseBody String find() throws Exception {
          throw new Exception("global_error_test");
        }
      }


      @Test
      public void testHandleException() throws Exception {
        mockMvc.perform(get("/tests/exception"))
            .andExpect(new ResultMatcher() {


              @Override
              public void match(MvcResult result) throws Exception {
                result.getResponse().getContentAsString().contains("global_error_test");
              }
            })
            .andExpect(status().isBadRequest());
      }

    /*
     * @Test public void testErrorDetailsValue() {
     * 
     * thrown.expect(Exception.class); thrown.expect(IOException.class);
     * thrown.expect(ParseException.class);
     * thrown.expect(JsonProcessingException.class);
     * 
     * thrown.expectMessage("Bad Request");
     * 
     * }
     */
}