PowerMock cannot resolve an ambiguous link

I am trying to test a simple application in Scala and test it with PowerMock.

Below is my code

Service.scala

trait Service {
    def getName(): String
    def start(): Int
}

ServiceListener.scala

trait ServiceListener {
  def onSuccess(service: Service): Unit
  def onFailure(service: Service): Unit
}

Somesystem.scala

import java.util
import java.util.List
import SomeSystem._

import scala.collection.JavaConversions._

object SomeSystem {

  def notifyServiceListener(serviceListener: ServiceListener, service: Service, success: Boolean) {
    if (serviceListener != null) {
      if (success) {
        serviceListener.onSuccess(service)
      } else {
        serviceListener.onFailure(service)
      }
    }
  }

  def startServiceStaticWay(service: Service): Int = {
    val returnCode = service.start()
    returnCode
  }
}

class SomeSystem {

  private val services: List[Service] = new util.ArrayList[Service]()
  private var serviceListener: ServiceListener = _
  private val events: List[String] = new util.ArrayList[String]()

  def start() {
    for (service <- services) {
      val something = startServiceStaticWay(service)
      val success = something > 0
      notifyServiceListener(serviceListener, service, success)
      addEvent(service, success)
    }
  }

  private def addEvent(service: Service, success: Boolean) {
    events.add(getEvent(service.getName, success))
  }

  private def getEvent(serviceName: String, success: Boolean): String = {
    serviceName + (if (success) "started" else "failed")
  }

  def add(someService: Service) {
    services.add(someService)
  }

  def setServiceListener(serviceListener: ServiceListener) {
    this.serviceListener = serviceListener
  }
}

I am trying to unit test SomeSystem.scala as below

import {ServiceListener, SomeSystem, Service}
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito
import org.powermock.api.mockito.PowerMockito
import org.powermock.modules.junit4.PowerMockRunner
//remove if not needed
import scala.collection.JavaConversions._

@RunWith(classOf[PowerMockRunner])
class PowerMockitoIntegrationTest {
  private var service: Service = _
  private var system: SomeSystem = _
  private var serviceListener: ServiceListener = _

  @Before
  def setupMock() {
    service = Mockito.mock(classOf[Service])
    serviceListener = Mockito.mock(classOf[ServiceListener])
    system = Mockito.spy(new SomeSystem())
    system.add(service)
    system.setServiceListener(serviceListener)
  }

  @Test
  def startSystem() {
    p("Stub using PowerMockito. service.start() should return 1 as we want start of the service to be successful")
    PowerMockito.when(service.start()).thenReturn(1)
    p("Start the system, should start the services in turn")
    system.start()
    p("Verify using Mockito that service started successfuly")
    Mockito.verify(serviceListener).onSuccess(service)
    p("Verifed. Service started successfully")
  }

  private def p(s: String) {
    println(s)
  }
}

Unfortunately, I get the following compilation error, I am confused why it appears and somehow we could get rid of it.

[ERROR] C:\IntellJWorkspace\PowerMockProblem\src\test\scala\PowerMockitoIntegrationTest.scala:29: error: ambiguous reference to overloaded definition,
[ERROR] both method when in object PowerMockito of type [T](x$1: T)org.mockito.stubbing.OngoingStubbing[T]
[ERROR] and  method when in object PowerMockito of type [T](x$1: Any, x$2: Object*)org.mockito.stubbing.OngoingStubbing[T]
[ERROR] match argument types (Int)
[ERROR]     PowerMockito.when(service.start()).thenReturn(1)
+4
source share
1 answer

When we use PowerMockitoin scala, we are faced with the problem of overload. The Scala compiler will not be able to correctly identify overloaded methods and will throw an error with an ambiguous definition.

, PowerMockito, , :

PowerMockito.when(SomeClass.staticmethod(someObject, someObject), Seq.empty[Object])
            .thenReturn(expectedXml, Seq.empty[Object])
+1

All Articles