2

我正在尝试使用 Scala 的 Wiremock 2.1.6。但是,映射构建器的类型发生了一些变化,因此 scalac 无法再对其进行类型检查。

文档中的第一个存根示例:

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

编译时导致此错误:

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(

该方法willReturnRemoteMappingBuilder接口中定义

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

在我看来,Scala 对RemoteMappingBuilderM extends RemoteMappingBuilder.

关于如何解决这个问题的任何建议?

4

2 回答 2

2

这已在最新版本中修复:https ://github.com/tomakehurst/wiremock/pull/482

于 2016-08-23T07:10:29.260 回答
2

你应该RemoteMappingBuilder[_,_]这样转换:

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

不幸的是,每次调用通用RemoteMappingBuilder接口的函数时都需要这样做,例如:

stubFor(get(urlEqualTo("/some/thing"))
    .withQueryParam("some_parameter", equalTo(paramValue)).asInstanceOf[RemoteMappingBuilder[_,_]]
    .willReturn(aResponse()
        .withHeader("Content-Type", "text/plain")
        .withBody("Hello world!")).asInstanceOf[RemoteMappingBuilder[_,_]])
于 2016-08-05T17:49:57.643 回答