Skip to content

Unable to use WebTestClient with mock server in Kotlin #20606

Closed
@spring-projects-issues

Description

@spring-projects-issues

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:

Referenced from: commits b9a0e6b

2 votes, 11 watchers

Metadata

Metadata

Assignees

Labels

in: testIssues in the test moduletype: taskA general task

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions