PowerMock无法解析不明确的引用



我正在尝试在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:IntellJWorkspacePowerMockProblemsrctestscalaPowerMockitoIntegrationTest.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)

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

因此,如果你想使用PowerMockito,请提及你的声明,如下所示,例如:

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

相关内容

  • 没有找到相关文章

最新更新