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

Unable to use WebTestClient with mock server in Kotlin

    XMLWordPrintable

    Details

    • Last commented by a User:
      true

      Description

      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)
                                  .apply(springSecurity())
                                  .configureClient()
                                  .filter(basicAuthentication())
                                  .build();
          }
      }
      

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

      <T extends B> T apply(MockServerConfigurer configurer)
      

      With the following code:

      WebTestClient.bindToApplicationContext(context)
                      .apply(springSecurity())
                      .configureClient()
                      .filter(basicAuthentication())
                      .build()
      

      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)
                      .apply<ApplicationContextSpec>(springSecurity())
                      .configureClient()
                      .filter(basicAuthentication())
                      .build()
      

      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.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              sdeleuze S├ębastien Deleuze
              Reporter:
              jonesd9 Daniel Jones
              Last updater:
              Spring Issues Spring Issues
              Votes:
              2 Vote for this issue
              Watchers:
              11 Start watching this issue

                Dates

                Created:
                Updated:
                Days since last comment:
                3 years, 37 weeks, 5 days ago