代码之家  ›  专栏  ›  技术社区  ›  Narendra Pandey

如何为返回PDF文件的Spring引导测试用例设置内容类型

  •  -3
  • Narendra Pandey  · 技术社区  · 7 年前

    我目前正在使用Spring引导测试测试测试我的一个服务。该服务导出所有用户数据,并在成功完成后生成一个csv或pdf。在浏览器中下载文件。

    下面是我在测试课上写的代码

    MvcResult result =   MockMvc.perform(post("/api/user-accounts/export").param("query","id=='123'")
        .contentType(MediaType.APPLICATION_JSON_VALUE)
        .accept(MediaType.APPLICATION_PDF_VALUE)
        .content(TestUtil.convertObjectToJsonBytes(userObjectDTO)))
        .andExpect(status().isOk())
        .andExpect(content().contentType(MediaType.APPLICATION_PDF_VALUE))
        .andReturn();
    String content = result.getResponse().getContentAsString();  // verify the response string.
    

    下面是我的资源类代码(调用到这个地方)

        @PostMapping("/user-accounts/export")
    @Timed
    public ResponseEntity<byte[]> exportAllUsers(@RequestParam Optional<String> query, @ApiParam Pageable pageable, 
    @RequestBody UserObjectDTO userObjectDTO) {
    HttpHeaders headers = new HttpHeaders();
    .
    .
    .
    
     return new ResponseEntity<>(outputContents, headers, HttpStatus.OK);
    
     }
    

    当我调试我的服务并将调试放在退出之前时,我将内容类型设置为“application/pdf”,状态设置为200。我尝试在我的测试用例中复制相同的内容类型。在执行过程中,它总是抛出低于错误的值-

       java.lang.AssertionError: Status 
       Expected :200
       Actual   :406
    

    我想知道,我应该如何检查我的反应(责任)。另外,响应所需的内容类型应该是什么。

    3 回复  |  直到 7 年前
        1
  •  1
  •   s7vr    7 年前

    你在别的地方有问题。似乎发生了一个异常/错误,如application/problem+json content type所指出的。这可能是在异常处理程序中设置的。因为您的客户只希望返回application/pdf 406。

    您可以添加一个测试用例来读取错误详细信息,以了解错误的确切内容。

    类似的东西

    MvcResult result = MockMvc.perform(post("/api/user-accounts/export").param("query","id=='123'")
        .contentType(MediaType.APPLICATION_JSON_VALUE)
        .accept(MediaType.APPLICATION_PROBLEM_JSON_VALUE)
        .content(TestUtil.convertObjectToJsonBytes(userObjectDTO)))
        .andExpect(status().isOk())
        .andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON_VALUE))
        .andReturn();
    String content = result.getResponse().getContentAsString();  // This should show you what the error is and you can adjust your code accordingly. 
    

    如果您期望出现错误,那么可以将accept类型更改为同时包含pdf和problem-json类型。

    注意-此行为取决于您拥有的SpringWebMVC版本。

    最新的SpringMVC版本考虑了响应实体中设置的内容类型头,忽略了接受头中提供的内容,并将响应解析为可能的格式。因此,您所做的相同测试不会返回406代码,而是返回带有应用程序JSON问题内容类型的内容。

        2
  •  0
  •   Jacob Botuck    7 年前

    406意味着您的客户机正在请求服务器认为无法提供的ContentType(可能是PDF)。

    我猜在调试时,您的代码工作的原因是您的REST客户机没有添加像测试代码那样请求PDF的接受头。

    若要解决此问题,请将添加到@postmaping注释中 produces = MediaType.APPLICATION_PDF_VALUE 看见 https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/PostMapping.html#produces--

        3
  •  0
  •   Narendra Pandey    7 年前

    我在@veeram的帮助下找到了答案,并了解到我的配置 MappingJackson2HttpMessageConverter 没有按我的要求。我覆盖它的默认支持 Mediatype 它解决了这个问题。

    默认支持-

    implication/json
    application*/json
    

    已完成代码更改以修复此案例-

    @Autowired
    private MappingJackson2HttpMessageConverter jacksonMessageConverter;
    
    List<MediaType> mediaTypes = new ArrayList<>();
    mediaTypes.add(MediaType.ALL);
    jacksonMessageConverter.setSupportedMediaTypes(mediaTypes);