如何通过Scalamock使用受保护的抽象方法将抽象Java类固态



我正在尝试使用抽象的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]

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

宏模型是要模拟类型的子类。因此,他们遵守与Scala中常规类层次结构相同的限制。您可以使用接口,例如 NetworkChannel并模拟?

拓宽方法的可见性的示例:

package java.nio.channels;
abstract class ServerSocketChannelSub extends ServerSocketChannel {
  def implCloseSelectableChannel(): Unit
  def implConfigureBlocking(x: Boolean): Unit
}

然后在您的测试中

val socketChan = mock[ServerSocketChannelSub]

构建此子类实例的所有副作用也将适用于每个模拟,没有办法围绕此。

相关内容

  • 没有找到相关文章

最新更新