PowerMock无法解决模糊引用

时间:2015-07-01 16:55:21

标签: scala mockito powermock easymock

我正在尝试在Scala中测试一个简单的应用程序,并使用PowerMock进行测试。

以下是我的代码

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
  }
}

我正在尝试对SomeSystem.scala进行单元测试,如下所示

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)
  }
}

不幸的是我收到了以下编译错误,我很困惑,为什么它出现了,我们可以做任何摆脱它。

[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)

1 个答案:

答案 0 :(得分:2)

当我们在scala中使用PowerMockito时,我们面临着重载问题。 Scala编译器无法正确识别重载方法,并抛出错误的定义。

因此,如果您想使用PowerMockito,请在下面提及您的陈述,例如:

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