如何使用 Mockito 在 Scala 对象中模拟函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/41663476/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to mock a function within Scala object using Mockito?
提问by Dilan
I am really new to Scala. I tried to mock a simple Scala function using Mockito, but I get the following error. I have checked the internet but I was unable to find out the error.
我对 Scala 真的很陌生。我尝试使用 Mockito 模拟一个简单的 Scala 函数,但出现以下错误。我已经检查了互联网,但我无法找出错误。
object TempScalaService {
  def login(userName: String, password: String): Boolean = {
    if (userName.equals("root") && password.equals("admin123")) {
      return true
    }
    else return false
  }
}
And my test class is below
我的测试课在下面
class TempScalaServiceTest extends FunSuite with MockitoSugar{
  test ("test login "){
    val service = mock[TempScalaService.type]
    when(service.login("user", "testuser")).thenReturn(true)
    //some implementation
  }
}
But I get the following error:
但我收到以下错误:
Cannot mock/spy class     com.pearson.tellurium.analytics.aggregation.TempScalaService$
Mockito cannot mock/spy following:
- final classes
- anonymous classes
- primitive types
org.mockito.exceptions.base.MockitoException: 
Cannot mock/spy class    com.pearson.tellurium.analytics.aggregation.TempScalaService$
Mockito cannot mock/spy following:
- final classes
- anonymous classes
- primitive types
   at  org.scalatest.mock.MockitoSugar$class.mock(MockitoSugar.scala:74)
    at    com.pearson.tellurium.analytics.aggregation.TempScalaServiceTest.mock(Temp    ScalaServiceTest.scala:7)
at     com.pearson.tellurium.analytics.aggregation.TempScalaServiceTest$$anonfun$    1.apply$mcV$sp(TempScalaServiceTest.scala:10)
    at    com.pearson.tellurium.analytics.aggregation.TempScalaServiceTest$$anonfun$    1.apply(TempScalaServiceTest.scala:9)
    at     com.pearson.tellurium.analytics.aggregation.TempScalaServiceTest$$anonfun$    1.apply(TempScalaServiceTest.scala:9)
    at    org.scalatest.Transformer$$anonfun$apply.apply$mcV$sp(Transformer.scala:    22)
    at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
采纳答案by LuisKarlos
You cannot mock objects, try to move your code to a class:
您不能模拟对象,请尝试将您的代码移动到一个类:
class TempScalaService() {
  def login(userName: String, password: String): Boolean = {
    if (userName.equals("root") && password.equals("admin123")) {
      return true
    }
    else return false
  }
}
and create a service:
并创建一个服务:
object TempScalaService {
   private val service = TempScalaService()
   def apply() = service
}
This would be better with a dependency injection framework, but it will work for now.
这对于依赖注入框架会更好,但它现在可以工作。
Now, for the test, use:
现在,对于测试,请使用:
val service = mock[TempScalaService]
when(service.login("user", "testuser")).thenReturn(true)
回答by Dan Simon
You can define the method in a trait which your object extends. Then simply mock the trait:
您可以在对象扩展的特征中定义方法。然后简单地模拟这个特征:
trait Login {
  def login(userName: String, password: String): Boolean
}
object TempScalaService extends Login {
   def login(userName: String, password: String): Boolean = {
     if (userName.equals("root") && password.equals("admin123")) {
   return true
   }
    else return false
  }
}
//in your test
val service = mock[Login]
回答by Gaurav Kumar
You can create a Scala Companion Object:
您可以创建一个 Scala 伴侣对象:
- Write test cases for you class.
 - Let that object do the external world interaction.
 
- 为您的班级编写测试用例。
 - 让那个对象做外部世界的交互。
 

