Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerMock not able to resolve ambiguous reference

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 am getting the below compilation error , I am confused why it is appearing and any way we could do 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)
like image 215
Stacker1234 Avatar asked Oct 19 '22 08:10

Stacker1234


1 Answers

When we use PowerMockito in scala, we face with overloading problem. Scala compiler will not be able to identify overloaded methods correctly and throws error saying ambiguous definition.

So if you want to use PowerMockito, mention your statement something as below, e.g.:

PowerMockito.when(SomeClass.staticmethod(someObject, someObject), Seq.empty[Object])
            .thenReturn(expectedXml, Seq.empty[Object])
like image 118
Sunil Bhat Avatar answered Oct 27 '22 10:10

Sunil Bhat