我正在使用spring test mvc框架为控制器编写测试.我正在尝试测试一个post请求,它会产生JSON.实际代码正在运行但测试失败.
我得到错误状态预期200但得到415这意味着不支持的媒体类型.我检查网上的所有例子,似乎我的实现是正确的.请帮帮我.
MyTestcase TestStringPost()运行成功,但TestJSONPost失败.
我使用的是Spring 3.2
我的控制器
@Controller
public class HomeController {
@RequestMapping(value = "/v1/checkstringvalue", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody void testStringPost(@RequestBody String value) {
logger.info("Welcome home! The client locale is {}.");
}
@RequestMapping(value = "/v1/checkjsonvalue", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody String testJSONPost(@RequestBody Map<String, String> userMap) {
System.out.println("Inside test jsonpost controller");
logger.info("Welcome home! The client locale is {}.");
return "Success";
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的测试控制器
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@WebAppConfiguration
public class HomeControllerTest {
@Autowired
private …Run Code Online (Sandbox Code Playgroud)