Test private methods with Scala Test

I will not debate upon whether writing private methods are a code smell or not but rather just show how a private method can be unit tested using the Scala Test framework

Here is our test class where we would write a unit test which tests a private method called myPrivateMethod in a class called MyClass

1class MyPrivateTester extends FlatSpec with Matchers with PrivateMethodTester {
2  "PrivateMethodTester" should "be able to test a private method in MyClass" in {
3    val privateMethod = PrivateMethod[String]('myPrivateMethod)
4    val myClass = new MyClass
5    val invocationResult: String = myClass.invokePrivate(privateMethod("param1", "param2"))
6  }
7} 

A few points to note about the test code is that we need to extend the PrivateMethodTester trait. The signature for a private method invocation looks like:

1val privateMethod = PrivateMethod[ReturyType_Of_The_Private_Method_That_Is_Tested]('Private_Method_Name')

The type PrivateMethod is a type member of the PrivateMethodTester trait. We give it the return type of what the private method is returning together with the private method name.

Once we have the PrivateMethod defined, we can use it to actually call our private method on the object or class type that we want ot test. In our example above, we wanted to test the private method named myPrivateMethod available in the MyClass object. Assuming that myPrivateMethod takes two String arguments, the call to test this method would look like:

1val invocationResult: String = myClass.invokePrivate(privateMethod("param1", "param2"))

The invocationResult is the result of calling the private method.