java.lang.AssertionError:内容类型未设置-Spring Controller Junit测试 [英] java.lang.AssertionError: Content type not set - Spring Controller Junit Tests

查看:1272
本文介绍了java.lang.AssertionError:内容类型未设置-Spring Controller Junit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对控制器进行一些单元测试.不管我做什么,所有控制器测试都会返回

I am trying to do some unit testing on my controllers. No matter what I do all controller tests return

java.lang.AssertionError: Content type not set

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

I am testing that the methods return json and xml data.

以下是控制器的示例:

@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

I am using: Spring 4.0.2 Junit 4.11 Gradle 1.12

我已经看到了这样的问题类似的问题,但是无论contentType和单元测试的预期组合是什么,我都会得到相同的结果.

I have seen the SO question Similiar Question but no matter what combination of contentType and expect in my unit test I get the same result.

任何帮助将不胜感激.

谢谢

推荐答案

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

Your solution depends on what kinds of annotation you want to use in your project.

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

  • You can add @ResponseBody to your getSchema method in Controller

或者,也许在您的@RequestMapping中添加produces属性也可以解决该问题.

Or, maybe adding produces attribute in your @RequestMapping can solve it too.

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

  • 最终选择,将标头添加到ResponseEntity(这是使用此类的主要目的之一)

  • Final choice, add headers to your ResponseEntity (which is one of the main objective of using this class)

    //...
    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属性:

    Edit : I've just seen you want Json AND Xml Data, so the better choice would be the produces attribute:

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

    这篇关于java.lang.AssertionError:内容类型未设置-Spring Controller Junit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆