【问题标题】:java.lang.AssertionError: Content type not set - Spring Controller Junit Testsjava.lang.AssertionError:未设置内容类型 - Spring Controller Junit 测试
【发布时间】:2014-06-11 14:55:35
【问题描述】:

我正在尝试对我的控制器进行一些单元测试。无论我做什么,所有控制器测试都会返回

java.lang.AssertionError: Content type not set

我正在测试这些方法是否返回 json 和 xml 数据。

这是一个控制器的例子:

@Controller
@RequestMapping("/mypath")

public class MyController {

   @Autowired
   MyService myService;

   @RequestMapping(value="/schema", method = RequestMethod.GET)
   public ResponseEntity<MyObject> getSchema(HttpServletRequest request) {

       return new ResponseEntity<MyObject>(new MyObject(), HttpStatus.OK);

   }

}

单元测试是这样设置的:

public class ControllerTest() { 

private static final String path = "/mypath/schema";
private static final String jsonPath = "$.myObject.val";
private static final String defaultVal = "HELLO";

MockMvc mockMvc;

@InjectMocks
MyController controller;

@Mock
MyService myService;

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);

    mockMvc = standaloneSetup(controller)
                .setMessageConverters(new MappingJackson2HttpMessageConverter(),
                        new Jaxb2RootElementHttpMessageConverter()).build();


    when(myService.getInfo(any(String.class))).thenReturn(information);
    when(myService.getInfo(any(String.class), any(Date.class))).thenReturn(informationOld);

}

@Test
public void pathReturnsJsonData() throws Exception {

    mockMvc.perform(get(path).contentType(MediaType.APPLICATION_JSON))
        .andDo(print())
        .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
        .andExpect(jsonPath(jsonPath).value(defaultVal));
}

}

我正在使用: 春天 4.0.2 六月 4.11 摇篮 1.12

我已经看到了 SO 问题 Similiar Question,但无论 contentType 和 expect 在我的单元测试中使用什么组合,我都会得到相同的结果。

任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: java spring spring-mvc junit mockmvc


    【解决方案1】:

    您的解决方案取决于您想在项目中使用哪种注释。

    • 您可以将@ResponseBody 添加到Controller 中的getSchema 方法中

    • 或者,也许在您的@RequestMapping 中添加produces 属性也可以解决它。

      @RequestMapping(value="/schema", 
            method = RequestMethod.GET, 
            produces = {MediaType.APPLICATION_JSON_VALUE} )
      
    • 最终选择,将标题添加到您的ResponseEntity(这是使用此类的主要目标之一)

      //...
      HttpHeaders headers = new HttpHeaders();
      headers.add("Content-Type", "application/json; charset=utf-8");
      return new ResponseEntity<MyObject>(new MyObject(), headers, HttpStatus.OK);
      

    编辑:我刚刚看到你想要 Json 和 Xml 数据,所以更好的选择是 produces 属性:

    @RequestMapping(value="/schema", 
          method = RequestMethod.GET, 
          produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE} )
    

    【讨论】:

    • 这有帮助,谢谢。我实际上正在将我的控制器分成两个控制器。一个带有 RestController 注释,它将 ResponseBody 添加到所有方法和第二个标准弹簧控制器。我引用了错误的控制器,因此 mockMvc.perform 返回 null 并且第一个期望失败,即内容检查。愚蠢的错误,但我想我会提到它以防其他人做同样的事情。
    • produces = {} 对我不起作用。我正在使用 Spring 4.3.2.RELEASE。解决了 response.setHeader("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE);
    • @söber,这很奇怪。您的请求的 accept 标头是否设置正确?
    • 在另一个部署的应用程序中,我看到请求标头:Accept:application/pdf 和响应标头:Content-Type:text/html;charset=utf-8。在 MockMVC 中,测试请求具有 Accept=[application/pdf] 并且响应具有标头 Content type = null。
    • 我通过确保控制器方法不返回 null 来修复此错误。
    【解决方案2】:

    你需要添加

    @RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE, 
    method = RequestMethod.GET
    value = "/schema")
    

    还有&lt;mvc:annotation-driven /&gt;在你的xml配置或@EnableWebMvc

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多