I believe the compiler errors you are seeing have to do with the fact that scalamock can not properly mock the PrintStream
class. If look at the scalamock scaladocs you will see the statement:
At present, ScalaMock can only mock traits, Java interfaces, and non-final
classes that define a default constructor
As the PrintStream
class is neither an interface nor does it have default constructor, my guess is that scalamock can not properly mock it and the errors you are seeing are a side effect of that. If you changed your code to use a OutputStream
instead (which is an interface and thus meets scalamock's restrictions), you could do your overloaded method mocking like this:
val mockStream = mock[OutputStream]
(mockStream.write(_:Int)) expects(1)
(mockStream.write(_:Array[Byte])) expects(Array[Byte](1,2,3))
Personally, I prefer Mockito used within Specs2 as it does not have these kinds of restrictions. An example of a class using PrintWriter
and then a test spec for that class using mocking with Mockito is as follows:
import java.io.PrintStream
import java.io.File
import org.specs2.mutable.Specification
import org.specs2.mock.Mockito
class MockitoExample extends Specification with Mockito{
val mockPrinter = mock[PrintStream]
val myPrinter = new MyPrintingClass{
override val printer = mockPrinter
}
"A request to print and attay of strings" should{
"call println on the PrintStream for each string supplied" in {
myPrinter print Array("foo", "bar")
there was one(mockPrinter).println("foo")
there was one(mockPrinter).println("bar")
}
}
}
class MyPrintingClass{
val printer = new PrintStream(new File("foo.txt"))
def print(strings:Array[String]) = strings foreach (printer.println(_))
}
Now this is a very trivial example, using only post-test verifications with no pre-test stubbings (because println
has a Unit
return type), but at least you can see that Mockito does not suffer from the same restrictions as scalamock. You can read more about using Mockito with Specs2 here.