【问题标题】:ScalaMock with scalatestScalaMock 与 scalatest
【发布时间】:2017-01-17 23:45:08
【问题描述】:

我是 scalatest 和 scalamock 的新手。这是我的 sbt 文件中的内容

name := "cakepattern"
version := "0.1"
scalaVersion := "2.11.8"
libraryDependencies ++= Seq(
  "org.scalatest" %% "scalatest" % "3.0.0" % "test",
  "org.scalamock" %% "scalamock-core" % "3.1.1" % "test",
  "org.scalamock" %% "scalamock-scalatest-support" % "3.1.1" % "test",
  "org.scalacheck" %% "scalacheck" % "1.13.0" % "test"
)

这是一个我试图模拟的类

package config

import dto.User
import services.AuthServiceComponent
import org.scalatest.mockito.MockitoSugar._
import services.impl.DefaultUserAuthServiceComponent


trait MockAuthServiceComponent extends AuthServiceComponent{

  val x = mock[AuthServiceLike]

  type AuthService = x.type
  override val userAuthService = x
}

当我做sbt test:compile 时,我收到以下错误

[error] missing or invalid dependency detected while loading class file 'MockitoSugar.class'.
[error] Could not access term mockito in package org,
[error] because it (or its dependencies) are missing. Check your build definition for
[error] missing or conflicting dependencies. (Re-run with `-Ylog-classpath` to see the problematic classpath.)
[error] A full rebuild may help if 'MockitoSugar.class' was compiled against an incompatible version of org.
[error] missing or invalid dependency detected while loading class file 'MockitoSugar.class'.
[error] Could not access type MockSettings in value org.mockito,
[error] because it (or its dependencies) are missing. Check your build definition for
[error] missing or conflicting dependencies. (Re-run with `-Ylog-classpath` to see the problematic classpath.)
[error] A full rebuild may help if 'MockitoSugar.class' was compiled against an incompatible version of org.mockito.
[error] missing or invalid dependency detected while loading class file 'MockitoSugar.class'.
[error] Could not access type Answer in value org.stubbing,
[error] because it (or its dependencies) are missing. Check your build definition for
[error] missing or conflicting dependencies. (Re-run with `-Ylog-classpath` to see the problematic classpath.)
[error] A full rebuild may help if 'MockitoSugar.class' was compiled against an incompatible version of org.stubbing.
[error] three errors found
[error] (test:compileIncremental) Compilation failed

我错过了什么?

[编辑]

所以我之前遇到的问题已经解决了,但现在我明白了

Error:scalac: missing or invalid dependency detected while loading class file 'AbstractMockFactory.class'.
Could not access type NoArgTest in trait org.scalatest.Suite,
because it (or its dependencies) are missing. Check your build definition for
missing or conflicting dependencies. (Re-run with `-Ylog-classpath` to see the problematic classpath.)
A full rebuild may help if 'AbstractMockFactory.class' was compiled against an incompatible version of org.scalatest.Suite.

有什么建议吗?

【问题讨论】:

    标签: scala scalatest scalamock


    【解决方案1】:

    尝试将 Mockito 添加到您的 sbt 文件中:

    libraryDependencies ++= Seq(
      "org.scalatest" %% "scalatest" % "3.0.0" % Test,
      "org.scalamock" %% "scalamock-core" % "3.1.1" % Test,
      "org.scalamock" %% "scalamock-scalatest-support" % "3.1.1" % Test,
      "org.scalacheck" %% "scalacheck" % "1.13.0" % Test,
      "org.mockito" % "mockito-all" % "1.10.19" % Test
    )
    

    注意,它是一个简单的“%”,而不是一个双倍的(它是一个 java 依赖)

    此处的其他版本:https://mvnrepository.com/artifact/org.mockito/mockito-all 如果 1.10.19 与您的代码库不兼容

    编辑:

    不确定这是否会帮助您解决第二个问题,但请在 SBT 控制台中尝试:

    > clean
    > compile
    

    【讨论】:

    • scalamock 是基于 mockito 之上的吗?
    • 不,scalamock 和 mockito 是两个不同的项目。我不明白您在测试用例中包含了这两个库。我会说删除测试用例中的“import org.scalatest.mockito.MockitoSugar._”行,并删除测试用例中的“”org.mockito”%“mockito-all”%“1.10.19”%Test”行build.sbt 文件,然后重试。另请参阅该页面:scalamock.org/user-guide/integration
    【解决方案2】:

    版本

    我建议更新到 scalamock 3.4.2(写作时的最新版本)。

    您始终可以在 Maven Central 上找到最新版本。

    另外,无需指定 scalamock-core,它会自动拉入。

    【讨论】:

      【解决方案3】:

      scalacheck 3.2 依赖于 scalatest 2.1.3,但您使用的是 scalatest 3.0.0,因此它们不兼容。删除最大规模的依赖将解决问题。

      【讨论】:

        猜你喜欢
        • 2015-07-25
        • 1970-01-01
        • 2013-02-14
        • 1970-01-01
        • 1970-01-01
        • 2017-10-17
        • 2014-01-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多