Spring MVC mocking

I am trying to use the Spring modular MVC module to unit test my controllers. I added:

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

which works great. However, I created a controller board that has a method annotated with @ExceptionHandler. I want to check that it works during unit testing. I saw that I can build an object MockMvcand pass it to standaloneSetup(..):

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

However, when I do this, a test that checks the operation of the exception handler passes, but all other tests that access the answer using jsonpath cannot execute this error:

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 to fix it? What's wrong?

The bottom line is that I want unit test my rest api. How to do this if I added an exception handler?

+3
source share
4

- , unit test my rest api. , , ?

spring, 3.2, ( , , , ). .

-, try and catch , , . , , 500, . api.

+1

accept():

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

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

, MediaType.

+1

null, spring mvc , . application/html + xml, application/json .., .

0

All Articles