gpt4 book ai didi

java - @ExceptionHandler 没有被调用测试

转载 作者:行者123 更新时间:2023-11-30 07:27:59 25 4
gpt4 key购买 nike

我一直在兜圈子试图修复测试,但 SO 或其他在线资源都没有提供解决方案。我有这个@ControllerAdvice处理 MyException 的方法异常(exception)是:

@ControllerAdvice
public class MyControllerAdvice {
@ExceptionHandler(MyException.class)
@ResponseBody
public HttpEntity<ErrorDetail> handleMyException(MyException exception) {
return new ResponseEntity<>(exception.getErrorDetail(), exception.getHttpStatus();
}
}

我有一个 Controller :

@Controller
@RequestMapping(value = "/image")
public class ImageController {
@Autowired
private MyImageService imageService;

@RequestMapping(value = "/{IMG_ID}", method = RequestMethod.GET,
produces = MediaType.IMAGE_PNG_VALUE)
public HttpEntity<?> getImage(String imageId) {
byte[] imageBytes = imageService.findOne(imageId); // Exception thrown here
....
return new ResponseEntity<>(imageBytes, HttpStatus.OK);
}
...
}

测试者:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyApplication.class)
@WebAppConfiguration
@IntegrationTest("server.port:0")
public class ThumbnailControllerTest {
@Autowired
private ImageController testObject;
private ImageService mockImageService = mock(ImageService.class);

@Autowired
protected WebApplicationContext webApplicationContext;
private MockMvc mockMvc;

@Before
public void setup() {
testObject.setImageService(mockImageService);
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}

@Test
public void testImageWhichDoesntExistReturns404() {
doThrow(new MyException("Doesn't exist", HttpStatus.NOT_FOUND))
.when(mockImageService).findOne(anyString());

mockMvc.perform(get("/image/doesnt_exist"))
.andExpect(status().isNotFound());
}
}

我对其他测试也有类似的设置,但这些似乎都通过了。然而对于这个我得到:Failed to invoke @ExceptionHandler method: public org.springframework.http.HttpEntity<mypackage.ErrorDetail>但是我知道它被调用,因为当我单步执行时它被调用并且日志显示它已被检测到( Detected @ExceptionHandler methods in MyControllerAdvice )。

我的想法是,这是因为 HttpMessageConverters 未正确解析,并尝试使用 ModelAndView 方法而不是所需的 JSON 格式来解析输出。我也无法通过使用 standaloneSetup 来强制执行此操作对于 MockMvc(使用 ControllerAdvice 和 HttpMessageConverters 集进行配置)或强制使用所需类型的 HttpMessageConverters bean。

我正在使用 spring 依赖项:

org.springframework.boot:spring-boot-starter-web:jar:1.3.1.RELEASE
org.springframework.boot:spring-boot-starter:jar:1.3.1.RELEASE
org.springframework.boot:spring-boot-starter-test:jar:1.3.1.RELEASE
org.springframework.boot:spring-boot-starter-data-rest:jar:1.3.1.RELEASE

我做错了什么?

最佳答案

我已经能够追踪到products = MediaType.IMAGE_PNG_VALUE。如果删除它,它就可以正常工作(假设您的 ErrorDetail 是 JSON 可序列化的)。问题是,AbstractMessageConverterMethodProcessor 坚持请求的类型。它只是跳过 JSON 转换器,因为它无法生成 image/png。指定 Produces = {MediaType.IMAGE_PNG_VALUE,MediaType.APPLICATION_JSON_VALUE}也没有帮助:它只是选择第一个类型并坚持使用它。

我一直无法弄清楚如何让它与 Produces 一起工作。欢迎任何改进或更正。

关于java - @ExceptionHandler 没有被调用测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36554452/

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