Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-16057

Unable to use WebTestClient with mock server in Kotlin


    • Last commented by a User:


      I'm trying to set up a Kotlin/Spring project using Spring Boot 2.0.0.M4 and Spring Framework 5.0.0.M4 and have ran into trouble with WebTestClient in a mocked-server test.

      Essentially the following in Java works fine:

      class JavaHelper {
          static WebTestClient getMockWebTestClient(ApplicationContext ctx) {
              return WebTestClient.bindToApplicationContext(ctx)

      But Kotlin is unable to infer the type T of apply method:

      <T extends B> T apply(MockServerConfigurer configurer)

      With the following code:


      The problem is to do with the generic typings, I'm still fairly new to Kotlin but if I write my test using the same package as ApplicationContextSpec (since they're package-private) and do the following, it works as expected:

      (WebTestClient.bindToApplicationContext(context) as ApplicationContextSpec)

      I think the following:

      static MockServerSpec<?> bindToApplicationContext(ApplicationContext applicationContext) {
          return new ApplicationContextSpec(applicationContext);

      should be changed to return ApplicationContextSpec (or at least AbstractMockServerSpec<ApplicationContextSpec>):

      and make the class ApplicationContextSpec public. The constructor can still be default visibility so users won't be able to misuse the class outside of the defined API, and users in Kotlin will be able to import it for type inference.


          Issue Links



              • Assignee:
                sdeleuze Sébastien Deleuze
                jonesd9 Daniel Jones
                Last updater:
                Sébastien Deleuze
              • Votes:
                1 Vote for this issue
                9 Start watching this issue


                • Created:
                  Days since last comment:
                  26 weeks, 3 days ago