运行springboot rest test时缺少头错误

时间:2017-07-22 13:34:27

标签: rest spring-boot integration-testing jhipster microservices

为使用JHipster(springboot)生成的微服务执行集成测试时出现奇怪错误

这是REST端点:

<input type="submit" />

这是失败的测试:

@RestController
@RequestMapping("/api")
public class MealResource {

    private MealService mealService;

    public MealResource(MealService mealService) {
        this.mealService = mealService;
    }

    @PostMapping("/meals")
    @Timed
    public ResponseEntity<Meal> createMeal(@Valid @RequestBody Meal meal) throws URISyntaxException {
        log.info("REST request to save Meal : {}", meal);
        String newMealId = mealService.save(meal);
        return ResponseEntity.created(new URI("/api/meals/" + newMealId))
            .headers(HeaderUtil.createEntityCreationAlert("meal", newMealId))
            .body(meal);
    }

这是错误消息:

 @Test
    public void createMealTest() throws Exception {
        restProfileMockMvc
            .perform(post("/api/meals")
            .header("Content-Type", "application/json")
            .contentType(TestUtil.APPLICATION_JSON_UTF8)
            .content(TestUtil.convertObjectToJsonBytes(createMeal())))
            .andExpect(status().isCreated())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE));
    }

我已经完成了一些调试,但我无法找到缺少的具体标题是什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

可能涉及到的人-您正在控制器中添加空标题。

您的

mealService.save(meal);

从控制器返回null,这就是断言失败的原因