【问题标题】:How to junit test a method with Mono return type如何对具有 Mono 返回类型的方法进行 junit 测试
【发布时间】:2021-11-11 15:00:13
【问题描述】:

junit mono 并获得身体响应的正确方法是什么? 我希望“仅单项”出现在 MockHttpServletResponse 的正文响应中。但相反,我看到它以异步方式返回。

样本方法:

public Mono<String> getData(final String data) {
    return this.webClient.get().uri("/dataValue/" + data)
        .retrieve()
        .bodyToMono(String.class);
}

Junit 示例:

@WebMvcTest(DataController.class)
public class DataMockTest {

  @Autowired
  private MockMvc mockMvc;

  @MockBean
  private Service service;

@Test
public void getDataTest() throws Exception {

Mono<String> strMono = Mono.just("Single Item Only");

when(service.getData("DATA")).thenReturn(strMono);

this.mockMvc.perform(get("/some/rest/toget/data")
    .contentType(MediaType.APPLICATION_JSON)
    .accept(MediaType.APPLICATION_JSON))
    .andDo(print())
    .andExpect(status()
        .isOk());
}

测试响应:

MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /some/rest/toget/data
       Parameters = {}
          Headers = [Content-Type:"application/json;charset=UTF-8", Accept:"application/json"]
             Body = null
    Session Attrs = {}

Async:
    Async started = true
     Async result = Single Item Only

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 200
    Error message = null
          Headers = []
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

使用 StepVerifier 更新了 Junit:

@WebMvcTest(DataController.class)
public class DataMockTest {

  @Autowired
  private MockMvc mockMvc;

  @MockBean
  private Service service;

@Test
public void getDataTest() throws Exception {

Mono<String> strMono = Mono.just("Single Item Only");

when(service.getData("DATA")).thenReturn(strMono);

//Added this which verifies the mono method. How to mock it so the API would return the mono value?
StepVerifier.create(service.getDATA("DATA"))
    .assertNext(data -> {
      assertNotNull(data);
      assertEquals("Single Item Only", data);
    }).verifyComplete();


this.mockMvc.perform(get("/some/rest/toget/data")
    .contentType(MediaType.APPLICATION_JSON)
    .accept(MediaType.APPLICATION_JSON))
    .andDo(print())
    .andExpect(status()
        .isOk());
}

【问题讨论】:

    标签: spring-boot spring-mvc mono spring-test mockmvc


    【解决方案1】:

    在我看来,您期待的是 StepVerifier。有了它,您可以断言 Mono 和 Flux 数据类型的内容。请参考https://www.baeldung.com/reactive-streams-step-verifier-test-publisher

    【讨论】:

    • 有没有一种方法可以让我模拟对我要测试的 api 执行获取?我能够使用 stepVerifier 来断言单声道数据。在帖子中添加。
    • 现在我很困惑。你想测试什么,你想模拟什么?你想断言响应的正文吗?
    • 是的,获取 api -> /some/rest/toget/data 应该返回一个正文响应。
    • 在这种情况下,您将需要使用“andExpect”,如baeldung.com/… 中所述。
    猜你喜欢
    • 2011-06-25
    • 1970-01-01
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 2019-11-17
    • 1970-01-01
    • 2014-05-10
    相关资源
    最近更新 更多