gpt4 book ai didi

java - ScalaMock 模拟通用 Java 接口(interface)重载方法

转载 作者:行者123 更新时间:2023-12-02 02:11:05 25 4
gpt4 key购买 nike

我正在尝试模拟具有不同数量参数的重载方法的 Java 通用接口(interface)。接口(interface)代码为:

import java.util.concurrent.Callable;

public interface GOInterface<T> {
void send(T record);
void send(T record, Callable<T> onComplete);
}

我尝试使用 onComplete 功能模拟发送,如下所示:

导入java.util.concurrent.Callable

import org.scalamock.scalatest.MockFactory
import org.scalatest.{FlatSpec, Matchers}

class JavaInterfaceTest extends FlatSpec with Matchers with MockFactory {
behavior of "scalamock"

it should "mock java generic interface with overloaded method (with different number of parameters)" in {
var result = ""
val m = mock[GOInterface[String]]
(m.send(_: String, _: Callable[String])).expects(*, *)
.onCall{ case(s: String, c: Callable[String]) => c.call()}.once

m.send("hello", new Callable[String] {
override def call(): String = {result = "world"; result}
})

result should be("world")
}


it should "mock java generic interface with overloaded method (with different number of parameters) 2" in {
var result = ""
val m = mock[GOInterface[String]]
(m.send(_: String)).expects(*).once

m.send("hello")

result should be("")
}
}

我从编译器得到的错误是:

error: value expects is not a member of (String, java.util.concurrent.Callable[String]) => Unit
[ERROR] (m.send(_: String, _: Callable[String])).expects(*, *)
[ERROR] ^

error: value expects is not a member of String => Unit
[ERROR] (m.send(_: String)).expects(*).once
[ERROR]

查看 ScalaMock 的不同示例 git我可以看到没有测试使用具有不同参数计数的重载方法来检查通用接口(interface)。

我的依赖项是:

        <dependency>
<groupId>org.scalamock</groupId>
<artifactId>scalamock-scalatest-support_2.11</artifactId>
<version>3.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scalamock</groupId>
<artifactId>scalamock-core_2.11</artifactId>
<version>3.6.0</version>
<scope>test</scope>
</dependency>

我创建了一个bug同时在 ScalaMock 存储库中。

最佳答案

我设法克服了这个问题。不是以最干净的方式,但它的作品。正如 @PhilippM 建议我需要修复类型,但不幸的是这还不够,我需要创建一个虚拟类。这是对我有用的解决方案:

class JavaInterfaceTest extends FlatSpec with Matchers with MockFactory {
behavior of "scalamock"

class StringInterface extends GOInterface[String] {
override def send(record: String): Unit = ()

override def send(record: String, onComplete: Callable[String]): Unit = ()
}

val call: (String, Callable[String]) => Unit = { case(s: String, c: Callable[String]) => c.call()}

it should "mock java generic interface with overloaded method (with different number of parameters)" in {
var result = ""
val m = mock[StringInterface]
(m.send(_: String, _: Callable[String])).expects(*, *)
.onCall{ call }.once

m.send("hello", new Callable[String] {
override def call(): String = {result = "world"; result}
})

result should be("world")
}


it should "mock java generic interface with overloaded method (with different number of parameters) 2" in {
var result = ""
val m = mock[StringInterface]
(m.send(_: String)).expects(*).once

m.send("hello")

result should be("")
}
}

我发现这有点丑陋,当需要模拟更复杂的接口(interface)时,这可能是最糟糕的,但我希望它对其他人有帮助。

关于java - ScalaMock 模拟通用 Java 接口(interface)重载方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50022484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com