从scala模拟Java静态类

时间:2017-11-03 13:24:50

标签: java scala java-8 mockito scalatest

任何想法如何从ScalaTest我可以模拟静态Java类?

我有这段代码

  val mockMapperComponent: IMapperComponent = mock[IMapperComponent]
  val applicationContext: ApplicationContext = mock[ApplicationContext]

  val appContextUtil: AppContextUtil = mock[AppContextUtil]


  override def beforeAll(): Unit = {
    mockStatic(classOf[AppContextUtil])
    when(AppContextUtil.getApplicationContext).thenReturn(applicationContext)
    when(applicationContext.getBean(classOf[IMapperComponent])).thenReturn(mockMapperComponent)

  }

在Java mockStatic中,类@PrepareForTest({AppContextUtil.class})中的注释可以解决问题,但是从Scala我只能在scalaTest文档中找到如何模拟正常访问,而不是静态访问。

问候。

0 个答案:

没有答案