首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用scalatest的ScalaMock

使用scalatest的ScalaMock
EN

Stack Overflow用户
提问于 2016-11-15 04:49:49
回答 3查看 2.8K关注 0票数 2

我刚接触scalatest和scalamock。下面是我在sbt文件中的内容

代码语言:javascript
复制
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"
)

下面是我想要模拟的一个类

代码语言:javascript
复制
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时,我得到以下错误

代码语言:javascript
复制
[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

我遗漏了什么?

编辑

所以我之前遇到的问题已经解决了,但现在我得到了这个

代码语言:javascript
复制
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.

有什么建议吗?

EN

回答 3

Stack Overflow用户

发布于 2016-11-15 04:56:31

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

代码语言:javascript
复制
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依赖项)

其他版本在这里:如果1.10.19与您的代码库不兼容,则为https://mvnrepository.com/artifact/org.mockito/mockito-all

编辑:

我不确定这是否会对您的第二个问题有所帮助,但可以在SBT控制台中尝试:

代码语言:javascript
复制
> clean
> compile
票数 5
EN

Stack Overflow用户

发布于 2017-01-17 23:45:08

版本低于3.3.0与Scalatest 3+不兼容。

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

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

而且,不需要指定scalamock-core,它会自动拉入。

票数 1
EN

Stack Overflow用户

发布于 2016-12-22 16:00:41

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40597594

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档