我是斯卡拉新手。目前我正在尋求對特徵進行模擬測試。有人能幫我實施嗎?我想模擬季節性SeasonalityServiceImpl和測試方法hMock.doSomeForF()中的方法seasonalityDao.doSomeForB和seasonalityDao.doSomeForB1。Mockito的斯卡拉測試特徵
更新: 我已根據下面的註釋更正了代碼。 (sMock).doSomeForB()驗證(sMock).doSomeForB1(),我收到下一個錯誤: 通緝但未調用: seasonalityDao.doSomeForB(); - > at Main $$ anonfun $ 1.apply $ mcV $ sp(Main.scala:12) 實際上,這個模擬與零交互。
下面是一個代碼(我剛纔簡化項目的顯著一部分爲例):
import org.scalatest._
import org.mockito.Mockito._
class Main extends FunSuite with SeasonalityServiceComponentImpl with SeasonalityDaoComponent {
test("some test") {
def hMock = mock(classOf[SeasonalityServiceImpl])
def sMock = mock(classOf[SeasonalityDao])
when(sMock.doSomeForB()).thenReturn(Option(2))
when(sMock.doSomeForB1()).thenReturn(10)
verify(sMock).doSomeForB()
verify(sMock).doSomeForB1()
println("With Option " + hMock.doSomeForF())
println("Without Option " + hMock.doSomeForF1())
}
override def seasonalityDao: SeasonalityDao = mock(classOf[SeasonalityDao])
override def seasonalityService: SeasonalityService = mock(classOf[SeasonalityService])
}
trait SeasonalityDaoComponent {
def seasonalityDao: SeasonalityDao
trait SeasonalityDao {
def doSomeForB(): Option[Int]
def doSomeForB1(): Int
}
}
trait SeasonalityServiceComponent {
def seasonalityService: SeasonalityService
trait SeasonalityService {
def doSomeForF(): Option[Int]
def doSomeForF1(): Int
}
}
trait SeasonalityServiceComponentImpl extends SeasonalityServiceComponent {
this: SeasonalityDaoComponent =>
trait SeasonalityServiceImpl extends SeasonalityService {
def doSomeForF(): Option[Int] = {
seasonalityDao.doSomeForB()
}
def doSomeForF1(): Int = {
seasonalityDao.doSomeForB1()
}
}
}
到底是什麼問題? –
如何正確實現SeasonalityServiceImpl的模擬測試?在這種情況下,我收到消息:不能嘲笑/間諜類主要$$不久$ 1 的Mockito不能嘲笑以下/間諜: - final類 - 匿名類 - 基本類型 org.mockito.exceptions.base.MockitoException :. .. – Nikolay
您應該編輯問題並添加這些錯誤。人們會更有可能回答。 至於問題本身,請使用'classOf [SeasonalityService]'而不是'seasonality.getClass'。這將使用接口,而不是您在下面返回的匿名類。 –