Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring boot: how to configure autowired WebTestClient

Are there any properties available in Spring Boot to configure the @Autowired WebTestClient? For instance, how to set the servlet context path (or just some base path for that matter) on WebTestClient?

Here's how my web tests are configured now:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
public class MyTestClass{

  @Autowired
  private WebTestClient cl;

  //the rest of it
}

In other words, what is Spring Boot equivalent of

WebTestClient client = WebTestClient.bindToServer()
    .baseUrl("http://localhost:<random port>/myServletContext").build();

I didn't find anything useful in documentation: https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

like image 908
badbishop Avatar asked Oct 28 '22 13:10

badbishop


1 Answers

building webTestClient using current application context, no need to hardcode uri and port number

    @Autowired
    ApplicationContext context;

    @Autowired
    WebTestClient webTestClient;

    @Before
    public void setup() throws Exception {

        this.webTestClient = WebTestClient.bindToApplicationContext(this.context).build();
    }
like image 155
jbaddam17 Avatar answered Nov 09 '22 17:11

jbaddam17