Java – spring MVC test result 415 error

I'm trying to write integration tests for rest APIs implemented using spring MVC

This is my rest implementation:

import org.myproject.api.input.ProjectInput;
import org.myproject.dao.ProjectsDao;
import org.myproject.model.Project;
import org.myproject.model.Projects;
import org.myproject.util.Exceptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/projects")
public class ProjectsApi {

    @Autowired
    private ProjectsDao projectsDao;

    ...

    @RequestMapping(value = "/",method = RequestMethod.POST,produces = {"application/json"},consumes = {"application/json"})
    public @ResponseBody Project addProject(@RequestBody ProjectInput projectInput) throws IOException {
        logger.info("Add project");
        Project project = projectInput.createProject();
        projectsDao.add(project);

        return project;
     }
}

This is the projectinput class:

@XmlRootElement
public class ProjectInput {
    private String name;
    private String description;

    // Constructor to make JSON converter happy
    private Projectinput() {}

    public ProjectInput(String name,String description) {
        this.name = name;
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }
    public void setName(String name) {
        this.name = name;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

This is my test:

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openboard.api.input.ProjectInput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.mockmvc;
import org.springframework.test.web.servlet.setup.mockmvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.request.mockmvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.mockmvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.mockmvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.mockmvcResultHandlers.print;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations={"classpath*:applicationContext.xml"})
public class TestProjectsApi {

    @Autowired
    private WebApplicationContext wac;

    private mockmvc mockmvc;

    @Before
    public void setup() {
        this.mockmvc = mockmvcBuilders.webAppContextSetup(this.wac).build();
    }

    @Test
    public void testAddProject() throws Exception {
        ProjectInput input = new ProjectInput("name","description");
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(input);
        // json value is: {"name":"name","description":"description"}

        mockmvc.perform(post("/projects/")
            .contentType(MediaType.APPLICATION_JSON)
            .content(json.getBytes()))
        .andDo(print())
        .andExpect(status().isOk())
        .andExpect(content().contentType(MediaType.APPLICATION_JSON));
    }
}

Unfortunately, I received the following error:

org.myproject.api.TestProjectsApi > testAddProject Failed
    java.lang.AssertionError: Status expected:<200> but was:<415>
        at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:60)
        at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:89)
        at org.springframework.test.web.servlet.result.StatusResultMatchers$10.match(StatusResultMatchers.java:653)
        at org.springframework.test.web.servlet.mockmvc$1.andExpect(mockmvc.java:152)
        at org.myproject.api.TestProjectsApi.testAddProject(TestProjectsApi.java:48)

I use the following gradle command to perform tests in the terminal:

./gradlew --daemon test --info

UPD. I added print () to the request to see what is being sent / received:

org.myproject.api.TestProjectsApi > testAddProject STANDARD_OUT

    MockHttpServletRequest:
             HTTP Method = POST
             Request URI = /projects/
              Parameters = {}
                 Headers = {Content-Type=[application/json]}

                 Handler:
                    Type = org.myproject.api.ProjectsApi

                   Async:
           Async started = false
            Async result = null

      Resolved Exception:
                    Type = org.springframework.web.HttpMediaTypeNotSupportedException

            ModelAndView:
               View name = null
                    View = null
                   Model = null

                FlashMap:

    MockHttpServletResponse:
                  Status = 415
           Error message = null
                 Headers = {Accept=[application/octet-stream,*/*,text/plain;charset=ISO-8859-1,application/xml,text/xml,application/*+xml,application/x-www-form-urlencoded,multipart/form-data]}
            Content type = null
                    Body = 
           Forwarded URL = null
          Redirected URL = null
                 Cookies = []
Gradle Test Executor 1 finished executing tests.

Solution

I have a similar situation. I can solve it by adding header accept and content type

Headers = {Accept=[application/json;charset=UTF-8],Content-Type=[application/json;charset=UTF-8]}

In the test module:

MediaType MEDIA_TYPE_JSON_UTF8 = new MediaType("application","json",java.nio.charset.Charset.forName("UTF-8"));
MockHttpServletRequestBuilder request = post("/myPostPath");
request.content(json);
request.locale(Locale.JAPANESE);
request.accept(MEDIA_TYPE_JSON_UTF8);
request.contentType(MEDIA_TYPE_JSON_UTF8);
mockmvc.perform(request)
    .andDo(print())
    .andExpect(status().isOk());

First of all, I only put request accept(..). But add request contentType(..) It finally worked

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>