Bjørn Madsen Bjørn Madsen - 3 months ago 26
Scala Question

Wiremock 2.x in Scala

I am trying to use Wiremock 2.1.6 from Scala. However, something has changed in the types of the mapping builders, so that scalac cannot typecheck it anymore.

The first stubbing example in the documentation:

stubFor(get(urlEqualTo("/some/thing"))
.willReturn(aResponse()
.withHeader("Content-Type", "text/plain")
.withBody("Hello world!")));


results in this error when compiling:

type mismatch;
found : ?0(in value <local TestSpec>) where type ?0(in value <local TestSpec>) <: AnyRef
required: com.github.tomakehurst.wiremock.client.RemoteMappingBuilder[_ <: AnyRef, _ <: com.github.tomakehurst.wiremock.client.ScenarioMappingBuilder]
get(urlEqualTo("some/thing")).willReturn(


The method
willReturn
is defined in the
RemoteMappingBuilder
interface

public interface RemoteMappingBuilder<M extends RemoteMappingBuilder, S extends ScenarioMappingBuilder> {
...
M willReturn(ResponseDefinitionBuilder responseDefBuilder);
}


It seems to me that Scala is not happy about the generic interface
RemoteMappingBuilder
being used without type parameters in
M extends RemoteMappingBuilder
.

Any suggestions on how to work around this?

Answer

You should cast to RemoteMappingBuilder[_,_] like this:

stubFor(get(urlEqualTo("/some/thing"))
    .willReturn(aResponse()
        .withHeader("Content-Type", "text/plain")
        .withBody("Hello world!")).asInstanceOf[RemoteMappingBuilder[_,_]])

Unfortunately this is needed every time calling functions of generic RemoteMappingBuilder interface, e.g.:

stubFor(get(urlEqualTo("/some/thing"))
    .withQueryParam("some_parameter", equalTo(paramValue)).asInstanceOf[RemoteMappingBuilder[_,_]]
    .willReturn(aResponse()
        .withHeader("Content-Type", "text/plain")
        .withBody("Hello world!")).asInstanceOf[RemoteMappingBuilder[_,_]])
Comments