Test private methods (a.k.a code smell) using 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

class MyPrivateTester extends FlatSpec with Matchers with PrivateMethodTester {
  "PrivateMethodTester" should "be able to test a private method in MyClass" in {
    val privateMethod = PrivateMethod[String]('myPrivateMethod)
    val myClass = new MyClass
    val invokationResult: String = myClass.invokePrivate(privateMethod("param1", "param2"))
  }
} 
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:
val 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 and 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:
val invokationResult: String = myClass.invokePrivate(privateMethod("param1", "param2"))
The invokationResult is the result of calling the private method.

0 comments: (+add yours?)

Post a Comment