首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SpringBoot2 +网络流量- WebTestClient返回“尚未提供的内容”

SpringBoot2 +网络流量- WebTestClient返回“尚未提供的内容”
EN

Stack Overflow用户
提问于 2018-03-28 09:25:29
回答 1查看 4.5K关注 0票数 4

我试图编写一些测试,当我尝试post字节数组主体时,我将面临以下异常:

错误

代码语言:javascript
复制
java.lang.AssertionError: Status expected:<201> but was:<404>

> POST /api/foo
> WebTestClient-Request-Id: [1]
> Content-Length: [246444]

Content not available yet

< 404 Not Found
< 

Content not available yet

我的测试类如下:

代码语言:javascript
复制
@AutoConfigureWebTestClient
@RunWith(SpringRunner.class)
@FixMethodOrder(NAME_ASCENDING)
@SpringBootTest(classes = Application.class)
public class ControllerTest {

  @Inject
  protected WebTestClient webClient;

  @Test
  public void testPostFile() throws Exception {

    byte[] bytes;

    try (InputStream is = getClass().getClassLoader().getResourceAsStream("static/binary-file.docx")) {
      bytes = IOUtils.toByteArray(is);
    }

    webClient
      .post()
      .uri("/api/foo")
      .body(BodyInserters.fromResource(new ByteArrayResource(bytes))))
      .exchange()
      .expectStatus().isCreated();

  }

}

是否有人面临同样的问题,似乎无法正确地加载资源。

编辑:

我的应用程序引导类

代码语言:javascript
复制
@EnableWebFlux
@Configuration
@ComponentScan(basePackageClasses = SBApplication.class)
@EnableAutoConfiguration
public class SBApplication {

    /**
     * Spring Boot Main Entry
     *
     * @param args command line arguments
     * @throws Exception on failure
     */
    public static void main(String[] args) throws Exception {

        ConfigurableApplicationContext ctx = new SpringApplicationBuilder()
                .sources(SBApplication.class)
                .run(args);
    }
}

我的控制员班:

代码语言:javascript
复制
@RestController
@RequestMapping("/api")
public class SBController {

  // ...

  @RequestMapping(
    method = POST,
    path = "{name}"
  )
  public Mono<ResponseEntity> store(
            @PathVariable("name") String name,
            @RequestBody byte[] body) {

    // do stuff

    return Mono.just(
      ResponseEntity
          .status(HttpStatus.CREATED)
          .build()
    );

  }

}
EN

回答 1

Stack Overflow用户

发布于 2018-04-03 16:40:29

最后,问题是我的Controller是通过注释配置的。WebTestClient只能识别通过RouterFunction路由的映射端点。

为了测试带有注释(如我的示例)的Controller,您需要初始化一个@AutoConfigureMockMvc并使用相应的注释@AutoConfigureMockMvc

最后,由于使用了reactor-core,我们需要使用MockMvcRequestBuilders.asyncDispatch,如下所示:

代码语言:javascript
复制
@AutoConfigureMockMvc
@RunWith(SpringRunner.class)
@FixMethodOrder(NAME_ASCENDING)
@SpringBootTest(classes = Application.class)
public class ControllerTest {

  @Inject
  protected MockMvc mockMvc;

  @Test
  public void testPostFile() throws Exception {

    byte[] bytes;

    try (InputStream is = getClass().getClassLoader().getResourceAsStream("static/binary-file.docx")) {
      bytes = IOUtils.toByteArray(is);
    }

    MvcResult result = mockMvc.perform(
                post("/api/foo")
                        .content(bytes)
        )
                .andExpect(request().asyncStarted())
                .andReturn();

        mockMvc.perform(asyncDispatch(result))
                .andExpect(status().isCreated());

  }

}

最后,一切都如期而至!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49530767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档