-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Unable to use WebTestClient with mock server in Kotlin #20606
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
Comments
Daniel Jones commented I've added a test repo here: https://github.com/dan-j/kotlin-reactive-test-SPR-16057 |
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 For now I am going to update |
Sébastien Deleuze commented Notice that #20251 is now fixed. |
Any work around? |
Hey, .webFilter<>(myfilter) . This is saying to give the generic type here. Error : Type expected val client: WebTestClient = WebTestClient.bindToWebHandler { Mono.empty() } .webFilter<>(myfilter) .build() Error : Type argument is not within its bounds. Expected: Nothing! Found: WebFilter! @sdeleuze can you help me in this |
@andriipivovarov Work around: You have to re-define class MutatorFilter (it is a private static class in // copy of org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.MutatorFilter
internal class MutatorFilter : WebFilter {
override fun filter(exchange: ServerWebExchange, webFilterChain: WebFilterChain): Mono<Void> {
val context = exchange.getAttribute<Supplier<Mono<SecurityContext>>>(ATTRIBUTE_NAME)
if (context != null) {
exchange.attributes.remove(ATTRIBUTE_NAME)
return webFilterChain.filter(exchange)
.subscriberContext(ReactiveSecurityContextHolder.withSecurityContext(context.get()))
}
return webFilterChain.filter(exchange)
}
companion object {
const val ATTRIBUTE_NAME = "context"
}
} And apply: WebTestClient.bindToApplicationContext(context)
.configureClient()
.baseUrl("https://api.example.com")
.defaultHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
// ...
.apply { _, httpHandlerBuilder, _ ->
httpHandlerBuilder?.filters { filters -> filters.add(0, MutatorFilter()) }
} If anyone knows about a better way, please let me know. |
This issue still happens with Kotlin 1.4.10 likely due to KT-40804 and I agree we should try to find a solution. I am discussing that with Kotlin team. |
Any updates on this? |
Both Kotlin and Spring team agreed this issue should be fixed on Kotlin side. My current hope is that it will be fixed in Kotlin 1.6 (Kotlin 1.5 is just around the corner and Kotlin has now a 6 month release cycle so that won't be too far away). |
Note that we (Kotlin team) supported given cases in the experimental mode in 1.5.30. In 1.5.30 the |
Indeed seems to work based on my tests, thanks! I will close this issue when we will be based on Kotlin 1.6 in order to add proper test. @petrukhnov Could you please confirm this will be the default as of Kotlin 1.6? |
Yes, it's going to be enabled by default since 1.6. |
Depends on #27413. |
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:
But Kotlin is unable to infer the type T of apply method:
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:I think the following:
should be changed to return
ApplicationContextSpec
(or at leastAbstractMockServerSpec<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
The text was updated successfully, but these errors were encountered: