Search code examples
scalaunit-testingmockitoguicescalatest

ScalaTest, Mockito, Guice and PartialMocking


I am using Google Guice as DI framework and I am writing Unit Tests for my classes which use Google Guice. I am also trying to do partial mocking.

Here is the code I wrote

class Test1 {
   def test1() = "I do test1"
}
class Test2 {
   def test2() = "I do test2"
}
class TestPartialMock @Inject()(t1: Test1, t2: Test2) {
   def test3() = "I do test3"
   def createList() : List[String] = List(t1.test1(), t2.test2(), test3())
}

My objective is to write a test case for the code above, but I ONLY want to mock test3

I wrote this test case

class TestModule extends AbstractModule with ScalaModule with MockitoSugar {
   override def configure() = {
      bind[Test1]
      bind[Test2]
      val x = mock[TestPartialMock]
      when(x.test3()).thenReturn("I am mocked")
      when(x.createList()).thenCallRealMethod()
      bind(classOf[TestPartialMock]).toInstance(x)
   }
}

class PartialMockTest extends FunSpec with Matchers {
   describe("we are testing workhorse but mock test3") {
      it("should return mock for test3") {
         val module = new TestModule
         val injector = Guice.createInjector(module)
         val tpm = injector.getInstance(classOf[TestPartialMock])
         val result = tpm.workHorse()
         result should contain ("I do test2")
         result should contain ("I do test1")
         result should contain ("I am mocked")
         result should not contain ("I do test3")
      }
   }
}

However the test fails with a null pointer exception on the dependencies (call to t1)

java.lang.NullPointerException
    at TestPartialMock.createList(TestPartialMock.scala:9)
    at PartialMockTest.$anonfun$new$2(PartialMockTest.scala:16)
    at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
    at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83)
    at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
    at org.scalatest.Transformer.apply(Transformer.scala:22)
    at org.scalatest.Transformer.apply(Transformer.scala:20)
    at org.scalatest.FunSpecLike$$anon$1.apply(FunSpecLike.scala:454)

So how can I have the injected dependencies along with the mocking for test3 method?

Here are my dependencies if you need to look at those

"net.codingwell" %% "scala-guice" % "4.1.0",
"org.scalatest" % "scalatest_2.12" % "3.0.3",
"org.scalamock" % "scalamock-scalatest-support_2.12" % "3.5.0",
"org.mockito" % "mockito-core" % "2.7.22"

Solution

  • The trick is that Mockito doesn't call the constructor of the (base) class when a mocked object is created. Thus the dependencies of the TestPartialMock are not initialized. The simplest way to work this around is to spy on a real object that you can create with whatever configuration you want

    class TestModule extends AbstractModule with ScalaModule with MockitoSugar {
      override def configure() = {
        //bind[Test1]
        //bind[Test2]
        //val x = mock[TestPartialMock]
        val realObject = new TestPartialMock(new Test1, new Test2)
        val x = spy(realObject)
        when(x.test3()).thenReturn("I am mocked")
        when(x.createList()).thenCallRealMethod()
        bind(classOf[TestPartialMock]).toInstance(x)
      }
    }