2016-10-26 24 views
0

我有一个Spring Boot REST应用程序。所有GET请求的单元测试工作正常;然而,在POST请求所有返回内容类型未设置Junit REST POST测试

java.lang.AssertionError: Content type not set 

这里是控制器:

@RestController 
public class ClassificationController { 

private IClassificationService classificationService; 

@Autowired 
public ClassificationController(IClassificationService classificationService) { 
    this.classificationService = classificationService; 
} 

@RequestMapping(value="/category", method = RequestMethod.POST, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}) 
@ResponseStatus(HttpStatus.CREATED) 
@ResponseBody 
public CategoryDTO createCategory(@RequestBody final CategoryDTO category) throws MctException { 
    return classificationService.createCategory(category); 
} 

单元测试我是:

@RunWith(MockitoJUnitRunner.class) 
public class ClassificationControllerTest { 

@Mock 
private IClassificationService classificationService; 

@Before 
public void setUp() { 
    mockMvc = MockMvcBuilders.standaloneSetup(new ClassificationController(classificationService)).build(); 
} 

@Test 
public void createCategoryTest() throws Exception { 
    String jsonTask = String.format("{\"id\": \"2\",\"categoryName\": \"Category Name 2\"}"); 
    MvcResult result = mockMvc.perform(post("/category") 
      .contentType(MediaType.APPLICATION_JSON_UTF8) 
      .content(jsonTask)) 
      .andDo(MockMvcResultHandlers.print()) 
      .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) 
      .andExpect(content().string(containsString("\"id\":2"))) 
      .andExpect(content().string(containsString("\"categoryName\":\"Category Name 2\""))) 
      .andExpect(status().isCreated()) 
      .andReturn(); 
} 

我还与CategoryDTO对象尝试这样做而不是具有相同结果的字符串jsonTask。

+0

您正在使用'MediaType.APPLICATION_JSON_UTF8_VALUE'和'.contentType(APPLICATION_JSON_UTF8)'。你不应该使用相同的吗? –

+0

其实,它是......我在玩选项时是一个定义的常量。当我粘贴代码时,我忘了将其改回。我会在上面更新它。 –

回答

1

我发现它只是在该断言失败,因为它是第一个,但它只是没有从端点返回任何东西。我正在返回内容类型,因为它正在返回正在插入的对象,因此内容类型是有效的。我最终改变我的测试使用ObjectMapper创建内容JSON,然后我必须在我的域对象上添加一个equals方法....一旦我添加了equals方法,测试就通过了。我没有意识到使用该方法的模拟框架。

@Test 
public void createClassTest() throws Exception { 
    String jsonInString = objectMapper.writeValueAsString(singleClass); 
    when(classificationService.createClass(5, singleClass)).thenReturn(singleClass); 
    MvcResult result = mockMvc.perform(post("/class/5") 
      .contentType(MediaType.APPLICATION_JSON_UTF8) 
      .content(jsonInString)) 
      .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) 
      .andExpect(content().string(containsString("\"id\":1"))) 
      .andExpect(content().string(containsString("\"className\":\"Test Class Name 1\""))) 
      .andExpect(status().isCreated()) 
      .andReturn(); 
    verify(classificationService).createClass(5, singleClass); 
} 
0

由断言错误判断,似乎端点没有返回MediaType.APPLICATION_JSON_UTF8。尝试删除contentType检查或调试,并查看端点实际返回的内容。再一次,根据您所看到的错误来判断,它似乎根本没有返回任何内容类型。所以你应该检查没有设置内容类型。

我知道通常我通常测试的POST请求根本不返回contentType。

毕竟,如果您确实希望设置内容类型,那么终端实际上​​可能会做些不正确的事情。