Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use WebTestClient with mock server in Kotlin [SPR-16057] #20606

Open
spring-issuemaster opened this Issue Oct 10, 2017 · 3 comments

Comments

Projects
None yet
1 participant
@spring-issuemaster
Copy link
Collaborator

spring-issuemaster commented Oct 10, 2017

Daniel Jones opened SPR-16057 and commented

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.


Affects: 5.0 GA

Issue Links:

  • #20945 Upgrade to Kotlin 1.3 ("depends on")
  • #20251 Kotlin unable to inherit type for WebTestClient#BodySpec

Referenced from: commits b9a0e6b

2 votes, 11 watchers

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Oct 10, 2017

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Oct 27, 2017

Sébastien Deleuze commented

I think this is similar to #20251 which was expected to be fixed in Kotlin 1.2 via KT-5464 and similar to what Rob Winch raised as well, but was sadly postponed to Kotlin 1.3. As reported to JetBrains, this pending issue on Kotlin side makes WebTestClient not usable at all with Kotlin, and I have no other workaround to propose than using WebClient with non-mocked server for now, Reactor and Spring Kotlin extensions making that quite usable as demonstrated on this example.

For now I am going to update WebTestClient Javadoc to add a warning and a link to JetBrains issue + update our reference documentation with these infos. We will mark this issue as resolved asap we have the confirmation Kotlin 1.3 fixes this issue and our documentation has been updated to specify Kotlin 1.3+ should be used for WebTestCient.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Apr 17, 2018

Sébastien Deleuze commented

Notice that #20251 is now fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.