Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking Scala Objects and Functions

I have a Scala object and it contains a few utility functions. These functions are called by other functions present in the same object or from other classes/objects. Is it possible to mock this object or the functions so that I can unit test the classes where they are being called.

Example:

object Util {

  def methodA() = { 
    //other code
    methodB() 
    //other code
  }

  def methodB() = { 
    //other code
    methodC() 
    //other code
  }

  def methodC() = { ... }

}

And here I'm calling the object function from another class

class Data {

  //other code

  def call() = {
    //other code
    Util.methodA()
    //other code
  }
}

How can I go about unit testing the function call() of class Data ? In Java I can create a mocked object for Util and set expectations for a call to methodA(), but this is not possible in Scala since no mocking library supports mocking of a Scala object.

like image 655
noober Avatar asked Apr 18 '17 15:04

noober


1 Answers

The problem in some sense is that you've used a pattern that is similar to a static class in Java. You can't mock static classes, which is why Java patterns often don't favor that approach.

On the other hand, in Scala you can. Make Util extend a trait and implement it.

trait UtilityMethods {
 def methodA()
 def methodB()
}

object Utils extends UtilityMethods {
  def methodA() = {}
  def methodB() = {}
}

And then in your test:

val mockedUtils = mock[UtilityMethods]

(using mockito). Obviously you will have to pass UtilityMethods around your code instead of Utils, just like you would wth an interface in Java.

In general, you can also use implicit scoping to inject functions and thus mock those dependencies without a static-like object approach, but the above is pretty close to Java patterns and should feel pretty comfortable.

like image 162
Keith Nordstrom Avatar answered Sep 28 '22 07:09

Keith Nordstrom