2017-05-15 126 views
0

我有一個返回DeferredResult的控制器,它是一個POST。使用MockMvc測試異步控制器

控制器:

@RequestMapping(value = "/{customerId}/cards", method = RequestMethod.POST) 
    @ResponseBody 
    public DeferredResult<CardSummaryQuery> createCard(
      @PathVariable String customerId, 
      @RequestBody CustomerDto payload) { 

     DeferredResult<CardSummaryQuery> deferred = new DeferredResult<>(); 

     Observable<CardSummaryQuery> observable = 
       cardService.registerCardObservable(customerId, payload); 
     observable.subscribe(deferred::setResult, deferred::setErrorResult); 
     return deferred; 
    } 

我想測試,但收到以下錯誤:「java.lang.AssertionError:內容類型未設置」

MvcResult result = mockMvc 
       .perform(post("/customers/55e5d-4df4r-v4fgt/cards").content(payload) 
         .contentType(MediaType.APPLICATION_JSON)) 
       .andExpect(status().isOk()).andExpect(request().asyncStarted()) 
       .andExpect(request().asyncResult("Deferred result")).andReturn(); 

     mockMvc.perform(asyncDispatch(result)).andExpect(jsonPath("$.success").value(true)); 

我能做些什麼來解決?

+0

也把你的控制器代碼 – pvpkiran

回答