Shashi
Shashi

Reputation: 41

Cannot invoke "java.lang.Integer.intValue()" specs2 mocking a method that returns int

I am using following configuration to run a test case using specs2 specification and specs2 mockito and mocked a method that is supposed to return int but it gives following error.

java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because the return value of "org.mockito.internal.creation.bytebuddy.MockMethodInterceptor$DispatcherDefaultingToRealMethod.interceptSuperCallable(Object, org.mockito.internal.creation.bytebuddy.MockMethodInterceptor, java.lang.reflect.Method, Object[], java.util.concurrent.Callable)" is null

Project configs.

Play Framework version: 2.8.19
scala version: 2.12.17
java version: 17
specs2-mock: 4.13.3
specs2-core: 4.13.3
scalacOptions:=Seq("-release", "17")
import com.google.inject.{Inject, Singleton}

@Singleton
class Demo @Inject() (f: Calculator) {

  def run(pair: Pair): Int = {
    val p = f.calculate(pair)
    p+5
  }

}
----------------------
import com.google.inject.{ Inject, Singleton }


@Singleton
class Calculator @Inject() (p: Int){

  def calculate(pair: Pair): Int = {
    p + pair.a
  }

}

case class Pair(a: Int, b: String)
---------------------------------------

import org.specs2.matcher.ShouldThrownExpectations
import org.specs2.mock.Mockito
import org.specs2.mutable.Specification
import org.specs2.specification.Scope

class DemoTest extends Specification  {

  isolated

  "DemoTest.calculate" should {

    "return 5" in new Mocks {

      f.calculate(any[Pair]) returns 6
      val res = demo.run(Pair(1,"str"))
      res shouldEqual 11
    }

  }

}

trait Mocks extends Scope with Mockito with ShouldThrownExpectations{
  val f = mock[Calculator]
  val demo = new Demo(f)
}

I've been investigating the issue, but I'm unable to pinpoint the exact cause here.

Upvotes: 0

Views: 185

Answers (0)

Related Questions