All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mockito.internal.handler.ScalaNullResultGuardian.scala Maven / Gradle / Ivy

The newest version!
package org.mockito
package internal.handler

import org.mockito.internal.util.Primitives.defaultValue
import org.mockito.invocation.{ Invocation, InvocationContainer, MockHandler }
import org.mockito.mock.MockCreationSettings

class ScalaNullResultGuardian[T](delegate: MockHandler[T]) extends MockHandler[T] {
  override def handle(invocation: Invocation): AnyRef = {
    val result = delegate.handle(invocation)

    val returnType = invocation.returnType

    if (result == null && returnType.isPrimitive)
      defaultValue(returnType).asInstanceOf[AnyRef]
    else if (result == null && invocation.returnsValueClass) {
      val runtimeReturnType = invocation.getRawReturnType
      if (runtimeReturnType.isPrimitive)
        defaultValue(runtimeReturnType).asInstanceOf[AnyRef]
      else
        DefaultAnswers.ReturnsSmartNulls(invocation).orNull.asInstanceOf[AnyRef]
    } else
      result
  }

  override def getMockSettings: MockCreationSettings[T]    = delegate.getMockSettings
  override def getInvocationContainer: InvocationContainer = delegate.getInvocationContainer
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy