2014-05-01 64 views
2

我正在为使用spring测试mvc框架的控制器编写一个测试。我试图测试一个发布请求,它会产生JSON。实际的代码正在运行,但测试失败。Spring MVC测试框架 - 不支持的媒体类型

我得到错误状态预期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"; 
} 
} 

这是我的测试控制器

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration 
@WebAppConfiguration 
public class HomeControllerTest { 


@Autowired 
private WebApplicationContext wac; 

private MockMvc mockMvc; 

private static final String PROVIDER_A = "PROVIDER_A"; 

public static final MediaType APPLICATION_JSON_UTF8 = new MediaType(
     MediaType.APPLICATION_JSON.getType(), 
     MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); 

@Configuration 
public static class TestConfiguration { 

    @Bean 
    public HomeController simpleController() { 
     return new HomeController(); 
    } 
} 

@Before 
public void setup() { 
    mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); 
} 

@Test 
public void testStringPost() throws Exception { 

    final String createJson = "hello"; 

    ResultActions resultActions = mockMvc.perform(post(
      "/v1/checkstringvalue").contentType(MediaType.TEXT_PLAIN) 
      .content(createJson)); 

    resultActions.andDo(print()); 
    resultActions.andExpect(status().isOk()); 
} 

@Test 
public void testjsonPost() throws Exception { 

    System.out.println("Inside testjsonpost"); 
    final String createJson = "{\"name\":\"hello\",\"email\":\"[email protected]\"}"; 

    ObjectMapper objectMapper = new ObjectMapper(); 
    ObjectNode userJson = objectMapper.createObjectNode(); 
    userJson.put("name", "tarun"); 
    userJson.put("email", "[email protected]"); 

    ResultActions resultActions = mockMvc 
      .perform(post("/v1/checkjsonvalue") 
        .contentType(APPLICATION_JSON_UTF8) 
        .accept(APPLICATION_JSON_UTF8) 
        .content(objectMapper.writeValueAsBytes(userJson))); 

    resultActions.andDo(print()); 
    resultActions.andExpect(status().isOk()); 
} 
} 
+0

尝试将'APPLICATION_JSON_UTF8'更改为简单MediaType.APPLICATION_JSON –

+0

嗨,techG,我已经完成了这项工作。 – vineet

+0

http://stackoverflow.com/questions/13782252/spring-mvc-3-content-type-application-json-works-from-client-but-not-from-unit可能有帮助 –

回答

0

测试代码:

ResultActions resultActions = mockMvc 
      .perform(post("/v1/checkjsonvalue") 
        .contentType(APPLICATION_JSON_UTF8) 
        .accept(APPLICATION_JSON_UTF8) 
        .content(objectMapper.writeValueAsBytes(userJson))); 

似乎添加contentType()头,但你的源代码并没有在@RequestMapping

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

一个consumes属性,我认为,如果你拿出的contentType()测试中的情况下或在源代码中添加一个消耗那么它可能工作。

+0

嗨,我已经alraedy做到了这一点。但仍然无法正常工作 – vineet

6

好了,所以具有

perform(post("/myapi").contentType(MediaType.APPLICATION_JSON).content("{\"mykey\":\"myvalue\"}")) 

是重要的。

还要检查你的控制器:如果缺少任何这些注释的,它不会工作:

@EnableWebMvc 
@Controller 
public class MyController { 
    @RequestMapping(value="/myapi", method=RequestMethod.POST) 
    public void postOnApi(@RequestBody MyWidget myWidget) { 
    } 
} 

的@EnableWebMvc @Controller和@RequestBody都需要(在我的实验)删除415状态码错误。

+3

@EnableWebMvc是我所缺失的。谢谢! –

相关问题