WebTestClient未注入

时间:2018-05-17 18:50:28

标签: spring spring-boot mocking integration-testing spring-webflux

首先,我是java堆栈的新手,在我的辩护中,我可能会问一些愚蠢的东西,谢谢你的耐心等待!

我需要什么:

集成测试但没有外部请求。这意味着我必须在堆栈中比正常单元测试更深的地方模拟依赖项。 我也不想在上下文中加载所有堆栈。

预期:

能够使用@BeanMock仅模拟客户端并进行测试通过。 (从我的试验来看,这只会嘲弄第一级的深度)。

实际

使用当前设置我正在

Error creating bean with name 'com.example.demo.SomeControllerTest': Unsatisfied dependency expressed through field 'webClient'

如果我使用@WebFluxTest(SomeController.class)ContextConfiguration(...),则客户端最终为空。如果我然后添加@TestConfiguration webflux抱怨在冲突@Configuration中有一些注释。

非常感谢任何想法!

@RestController
public class SomeController {
  private final SomeService someService;

  @Autowired
  public SomeController(SomeService someService) {
    this.someService = someService;
  }


  @GetMapping(value = "/endpoint")
  public Mono<String> endpoint() {
    return someService.get();
  }
}


@Service
public class SomeService {
  private final Client client;

  @Autowired
  public SomeService(Client client) {
    this.client = client;
  }

  public Mono<String> get() {
    return client.build().get().retrieve().bodyToMono(String.class);
  }
}


@Component
public class Client {
  private final HttpServletRequest request;

  @Autowired
  public Client(HttpServletRequest request) {
    this.request = request;
  }

  public WebClient build() {
    return WebClient.builder()
      .baseUrl("https://httpstat.us/200")
      .build();
  }
}

@RunWith(SpringRunner.class)
@TestConfiguration
@SpringBootTest(classes = {
  SomeController.class,
  SomeService.class
})
@AutoConfigureWebTestClient
public class SomeControllerTest {
  @Autowired
  private WebTestClient webClient;

  @MockBean
  private Client client;


  @Before
  public void setUp() {
    when(client.build())
      .thenReturn(WebClient.create("https://httpstat.us/201"));
  }

  @Test
  public void deepMocking() {
    webClient.get()
      .uri("/endpoint")
      .exchange()
      .expectStatus().isOk()
      .expectBody(String.class).isEqualTo("201 Created");
  }
}

3 个答案:

答案 0 :(得分:1)

@RunWith(SpringRunner.class)
@WebFluxTest(SomeController.class)
@ContextConfiguration(classes = {
   SomeController.class,
   SomeService.class
 })
 public class SomeControllerTest {
   @Autowired
   private WebTestClient webClient;
   // ...
 }

这是必需的组合,但我不明白为什么需要将SomeController.class添加到活动上下文中,同时也不@WebFluxTest这样做?< / p>

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {
  SomeController.class,
  SomeService.class,
})
// @AutoConfigureWebTestClient // does absolutely nothing?
public class SomeControllerTest {
  @Autowired
  private SomeController controller;

  // ...

  @Test
  public void deepMocking() {
    WebTestClient.bindToController(controller)
      .build()
      .get()
      .uri("/endpoint")
      .exchange()
      .expectStatus().isOk()
      .expectBody(String.class).isEqualTo("201 Created");
  }
}

答案 1 :(得分:1)

经过03天的苦苦挣扎,这就是我解决问题的方式:

感谢llooottt:https://www.baeldung.com/spring-5-webclient

@WebFluxTest
public class AnimeControllerIntegrTest{

    WebTestClient testClient;

    @Test
    public void get_RA() {

        testClient = WebTestClient.bindToServer().baseUrl("http://localhost:8080/animes").build();

        RestAssuredWebTestClient
                .given()
                .webTestClient(testClient)

                .when()
                .get()

                .then()
                .statusCode(OK.value())

                .body("name" ,hasItem("paulo"))
        ;
    }
}

答案 2 :(得分:0)

错误表明您在尝试自动装配时遇到错误 WebTestClient类中的SomeControllerTest bean,你不能只是将WebTestClient自动装入它必须改为

WebTestClient testClient = WebTestClient
  .bindToServer()
  .baseUrl("http://localhost:8080")
  .build();

您可以在本文中找到有关WebClient的更多信息 http://www.baeldung.com/spring-5-webclient