【问题标题】:Content Type Not Set Junit REST POST test内容类型未设置 Junit REST POST 测试
【发布时间】:2017-03-09 10:46:06
【问题描述】:

我有一个 Spring Boot REST 应用程序。所有 GET 请求的单元测试都运行良好;但是,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 对象而不是 String jsonTask,结果相同。

【问题讨论】:

  • 您正在使用MediaType.APPLICATION_JSON_UTF8_VALUE.contentType(APPLICATION_JSON_UTF8)。你不应该用同样的吗?
  • 实际上,它是... one 是我在玩选项时定义的常量。我只是在粘贴代码时忘记将其更改回来。我会在上面更新它。

标签: rest unit-testing junit spring-boot


【解决方案1】:

从断言错误来看,似乎端点没有返回 MediaType.APPLICATION_JSON_UTF8。尝试删除 contentType 检查或调试并查看端点实际返回的内容。再一次,从您看到的错误来看,它似乎根本没有返回任何内容类型。因此,您可能应该检查是否未设置任何内容类型。

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

毕竟,如果您确实希望设置内容类型,那么端点实际上可能做错了什么。

【讨论】:

    【解决方案2】:

    我发现它只是在那个断言上失败了,因为它是第一个断言,但它只是没有从端点返回任何东西。我正在返回内容类型,因为它正在返回正在插入的对象,因此内容类型是有效的。我最终更改了我的测试,使用 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-16
      • 1970-01-01
      • 1970-01-01
      • 2015-11-09
      • 1970-01-01
      • 2019-03-03
      • 2019-06-13
      相关资源
      最近更新 更多