我正在尝试使用抽象的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]
构建此子类实例的所有副作用也将适用于每个模拟,没有办法围绕此。