简体   繁体   English

Spring MVC嘲笑

[英]Spring MVC mocking

I am trying to use Spring MVC mock module to unit test my controllers. 我正在尝试使用Spring MVC模拟模块对我的控制器进行单元测试。 I have added this: 我添加了这个:

    @Before
    public void beforeTest() {
        MockitoAnnotations.initMocks(this);
        standaloneSetup(new CarController());
    }

which works fine. 哪个工作正常。 However I have created a controller advice that have a method annotated with @ExceptionHandler . 但是我创建了一个控制器建议,其中有一个使用@ExceptionHandler注释的方法。 I want to test that it works during unit testing. 我想测试它在单元测试期间是否有效。 I saw that I can build a MockMvc object and pass that to standaloneSetup(..) : 我看到我可以构建一个MockMvc对象并将其传递给standaloneSetup(..)

MockMvcBuilders.standaloneSetup(
    new CarController()).setHandlerExceptionResolvers(...).build();

However when I do this the test that checks that the exception handler works passes but all the other tests that access the response with jsonpath fails with this error: 但是当我这样做时,检查异常处理程序工作的测试会通过但是使用jsonpath访问响应的所有其他测试都会失败,并显示以下错误:

java.lang.IllegalStateException: Expected response body to be verified as JSON,
    HTML or XML but content-type 'null' is not supported out of the box.
Try registering a custom parser using:
   RestAssured.registerParser("null", <parser type>);
Content was:

How do I fix this? 我该如何解决? What is wrong? 怎么了?

Bottom line is that I want to unit test my rest api. 底线是我想要对我的休息api进行单元测试。 How should I do this when I have added a exception handler for instance? 当我为实例添加异常处理程序时,我该怎么做?

Bottom line is that I want to unit test my rest api. 底线是我想要对我的休息api进行单元测试。 How should I do this when I have added a exception handler for instance? 当我为实例添加异常处理程序时,我该怎么做?

For one I would use the built in spring mock framework, available from version 3.2, and not rest assured (it might be good, but can;t imagine it is integrated as well). 首先,我将使用内置的spring模拟框架,可从版本3.2获得,并且不放心(它可能很好,但可以想象它也是集成的)。 This might cover the basics of getting started . 这可能涉及的基础知识入门

Secondly, you can just try and catch in your test method, then assert on a boolean as to whether a exception was thrown. 其次,您可以try and catch测试方法,然后在布尔值上断言是否抛出异常。 Or test for whether an exception message was returned from controller doing an expect on status code 500, for example. 或者测试是否从控制器返回异常消息,例如,对状态代码500执行期望。 This will depend on your api. 这取决于你的api。

Try using accept(): 尝试使用accept():

mockMvc.perform(post("/hotels/{id}", 42).accept(MediaType.APPLICATION_JSON));

You can also set the return type when building MockMvc. 您还可以在构建MockMvc时设置返回类型。

mockMvc = standaloneSetup(new AccountController())
        .defaultRequest(get("/")
        .contextPath("/app").servletPath("/main")
>>        .accept(MediaType.APPLICATION_JSON).build();

http://docs.spring.io/spring/docs/4.0.1.RELEASE/spring-framework-reference/htmlsingle/#unit-testing-spring-mvc http://docs.spring.io/spring/docs/4.0.1.RELEASE/spring-framework-reference/htmlsingle/#unit-testing-spring-mvc

You will need to pass in the proper MediaType value of course. 当然,您需要传递正确的MediaType值。

This link might help you since the author is also showing how to test rest api for an expected exception, among other tests. 此链接可能对您有所帮助,因为作者还展示了如何测试其他测试中的预期异常的rest api。

http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-mvc-controllers-rest-api/ http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-mvc-controllers-rest-api/

The mocked content type of the request is null, so spring mvc cannot determine which method it should invoke. 请求的模拟内容类型为null,因此spring mvc无法确定应调用哪个方法。 If you set the value to application/html+xml, application/json, etc it should work correctly. 如果将值设置为application / html + xml,application / json等,它应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM