I believe that the compiler errors you see are related to the fact that scalamock cannot correctly mock the PrintStream class. If you look at the scaladocs, you will see instructions:
At present, ScalaMock can only mock traits, Java interfaces, and non-final classes that define a default constructor
Since the PrintStream class is not an interface and does not have a default constructor, I assume that the skalamok cannot mock it correctly, and the errors you see are a side effect of this. If you changed your code to use an OutputStream instead (which is an interface and therefore conforms to the restrictions on skymac), you can make your overloaded method, taunting as follows:
val mockStream = mock[OutputStream] (mockStream.write(_:Int)) expects(1) (mockStream.write(_:Array[Byte])) expects(Array[Byte](1,2,3))
Personally, I prefer to use Mockito in Specs2 , as it has no such restrictions. An example class using PrintWriter and then a test specification for this class using the mock with Mockito looks like this:
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 checks without preliminary tests (because println has a return type of Unit ), but at least you can see that Mockito does not suffer from the same limitations as the skalamok. You can learn more about using Mockito with Specs2 here .
cmbaxter
source share