我正在尝试将抽象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]
当然我可以在测试子类中覆盖这些方法但是可能有更优雅的解决方案吗?
答案 0 :(得分:0)
宏模拟是要模拟的类型的子类。所以他们遵守与Scala中常规类层次结构相同的限制。
您可以使用接口,而不是直接依赖抽象类。 NetworkChannel
并嘲笑那个?
扩大方法可见性的示例:
package java.nio.channels;
abstract class ServerSocketChannelSub extends ServerSocketChannel {
def implCloseSelectableChannel(): Unit
def implConfigureBlocking(x: Boolean): Unit
}
然后在你的测试中
val socketChan = mock[ServerSocketChannelSub]
构造这个子类的一个实例的所有副作用也适用于每个模拟,没有办法解决这个问题。