java http 415错误_java – Spring MVC测试结果415错误

我正在尝试为使用

Spring MVC实现的REST API编写集成测试.

这是我的REST实现:

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;

}

}

这是ProjectInput类:

@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;

}

}

这是我的测试:

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));

}

}

不幸的是我收到以下错误:

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)

我使用以下Gradle命令在终端中执行测试:

./gradlew --daemon test --info

UPD.我在请求中添加了print()以查看正在发送/接收的内容:

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值