Java 如何测试@ControllerAdvice引用的注释的自定义异常处理程序?

Java 如何测试@ControllerAdvice引用的注释的自定义异常处理程序?,java,spring-boot,testing,exception,Java,Spring Boot,Testing,Exception,我正在一家基于的服务代理上工作。我正在使用从ServiceBrokerExceptionHandler继承的自定义ExceptionHandler来调整所有ServiceBrokerRestController上特定情况下的HTTP状态代码: @ControllerAdvice(annotations=ServiceBrokerRestController.class) 公共类MyServiceBrokerExceptionHandler扩展ServiceBrokerExceptionHandl

我正在一家基于的服务代理上工作。我正在使用从
ServiceBrokerExceptionHandler
继承的自定义ExceptionHandler来调整所有ServiceBrokerRestController上特定情况下的HTTP状态代码:

@ControllerAdvice(annotations=ServiceBrokerRestController.class)
公共类MyServiceBrokerExceptionHandler扩展ServiceBrokerExceptionHandler{
@ExceptionHandler({MyCustomException.class})
@ResponseStatus(HttpStatus.INTERNAL\u SERVER\u错误)
公共错误消息handleException(MyCustomException ex){
返回此.getErrorResponse(ex);
}
}

现在我想测试它,但我真的不明白怎么做。由于处理程序应用于所有带有
@ServiceBrokerRestController
注释的控制器,因此我并不真正关心某个控制器bean,更不用说某个方法了。我只是想“模拟”某种ServiceBrokerRestController,并给出它的一个方法抛出一个
MyCustomException
web层应该返回500-内部服务器错误。我必须为此创建ServiceBrokerRestController虚拟,还是有更好的方法?

它应该是一个简单的集成,如下所示。您可以在测试包中创建一个虚拟控制器,它只抛出您想要测试的异常

虚拟控制器:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DummyExceptionTestController {
    @GetMapping("/my-exception")
    public void customException() {
        throw new MyCustomException("My Error Message 1");
    }
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@AutoConfigureMockMvc
@SpringBootTest(classes = DemoApplication.class)
public class ExceptionHandlerIT {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void testMyException() throws Exception {
        mockMvc.perform(get("/my-exception"))
                .andExpect(status().isBadRequest())
                .andExpect(content().string("My Error Message 1"));
    }


}
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class MyServiceBrokerExceptionHandler {

    @ExceptionHandler({MyCustomException.class})
    public ResponseEntity<String> handleException(MyCustomException ex) {
        return ResponseEntity.badRequest().body(ex.getMessage());
    }

}

public class MyCustomException extends RuntimeException {
    public MyCustomException(String msg) {
        super(msg);
    }
}
集成测试:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DummyExceptionTestController {
    @GetMapping("/my-exception")
    public void customException() {
        throw new MyCustomException("My Error Message 1");
    }
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@AutoConfigureMockMvc
@SpringBootTest(classes = DemoApplication.class)
public class ExceptionHandlerIT {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void testMyException() throws Exception {
        mockMvc.perform(get("/my-exception"))
                .andExpect(status().isBadRequest())
                .andExpect(content().string("My Error Message 1"));
    }


}
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class MyServiceBrokerExceptionHandler {

    @ExceptionHandler({MyCustomException.class})
    public ResponseEntity<String> handleException(MyCustomException ex) {
        return ResponseEntity.badRequest().body(ex.getMessage());
    }

}

public class MyCustomException extends RuntimeException {
    public MyCustomException(String msg) {
        super(msg);
    }
}
异常处理程序和异常:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DummyExceptionTestController {
    @GetMapping("/my-exception")
    public void customException() {
        throw new MyCustomException("My Error Message 1");
    }
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@AutoConfigureMockMvc
@SpringBootTest(classes = DemoApplication.class)
public class ExceptionHandlerIT {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void testMyException() throws Exception {
        mockMvc.perform(get("/my-exception"))
                .andExpect(status().isBadRequest())
                .andExpect(content().string("My Error Message 1"));
    }


}
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class MyServiceBrokerExceptionHandler {

    @ExceptionHandler({MyCustomException.class})
    public ResponseEntity<String> handleException(MyCustomException ex) {
        return ResponseEntity.badRequest().body(ex.getMessage());
    }

}

public class MyCustomException extends RuntimeException {
    public MyCustomException(String msg) {
        super(msg);
    }
}
import org.springframework.web.bind.annotation.ControllerAdvice;
导入org.springframework.web.bind.annotation.ExceptionHandler;
@控制器建议
公共类MyServiceBrokerExceptionHandler{
@ExceptionHandler({MyCustomException.class})
公共响应handleException(MyCustomException ex){
返回ResponseEntity.badRequest().body(例如getMessage());
}
}
公共类MyCustomException扩展了RuntimeException{
公共MyCustomException(字符串消息){
超级(味精);
}
}