0

I'm trying to stub abstract java.nio.channels.ServerSocketChannel class but got

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]

Of course I can override these methods in test subclass but maybe there is a more elegant solution?

Andrei Tanana
  • 7,932
  • 1
  • 27
  • 36

1 Answers1

0

Macro Mocks are subclasses of the type to mock. So they abide by the same restrictions as regular class hierarchies in Scala. Instead of depending on the abstract class directly, could you use an interface, e.g. NetworkChannel and mock that ?

example for widening the visibility of a method:

package java.nio.channels;

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

then in your test

val socketChan = mock[ServerSocketChannelSub]

All side-effects of constructing an instance of this subclass will also apply on each mock, there is no way around this.

Philipp
  • 967
  • 6
  • 16
  • Sorry, not my case. I need exactly `ServerSocketChannel` methods – Andrei Tanana Apr 24 '17 at 20:55
  • Well then you need to be quite careful. Any side-effects that run in the constructor will also run for the mock. To be able to mock the type, you can create a (still abstract if you like) subclass that widens the visibility of the package protected methods to public. I'll edit the answer above with an example. – Philipp Apr 25 '17 at 16:58
  • Thanks! I'm already did the same and it works fine. Just asked for more elegant solution but it seems not exist for current ScalaMock api(3.5.0). – Andrei Tanana Apr 26 '17 at 18:13