0

我正在尝试存根抽象java.nio.channels.ServerSocketChannel类但得到了

Error:(15, 18) object creation impossible, since:
it has 2 unimplemented members.
/** As seen from <$anon: java.nio.channels.ServerSocketChannel>, the missing signatures are as follows.
 *  For convenience, these are usable as stub implementations.
 */
  protected[package spi] def implCloseSelectableChannel(): Unit = ???
  protected[package spi] def implConfigureBlocking(x$1: Boolean): Unit = ???
    socket = stub[ServerSocketChannel]

当然我可以在测试子类中覆盖这些方法,但也许有更优雅的解决方案?

4

1 回答 1

0

宏 Mocks 是要模拟的类型的子类。因此它们遵守与 Scala 中常规类层次结构相同的限制。除了直接依赖抽象类,您可以使用接口,例如NetworkChannel并模拟它吗?

扩大方法可见性的示例:

package java.nio.channels;

abstract class ServerSocketChannelSub extends ServerSocketChannel {
  def implCloseSelectableChannel(): Unit
  def implConfigureBlocking(x: Boolean): Unit
}

然后在你的测试中

val socketChan = mock[ServerSocketChannelSub]

构造这个子类的实例的所有副作用也将适用于每个模拟,没有办法解决这个问题。

于 2017-04-24T07:09:32.380 回答