简体   繁体   English

Scalatest的ScalaMock

[英]ScalaMock with scalatest

I am new to scalatest and scalamock. 我是scalatest和scalamock的新手。 Here is what I have it in my sbt file 这是我的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"
)

And here is a class that I am trying to mock 这是我要模拟的课程

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
}

When I am do sbt test:compile I get the following error 当我做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

What am I missing? 我想念什么?

[Edit] [编辑]

So the problem I was having before is resolved but now I get this 所以我以前遇到的问题已经解决,但现在我明白了

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.

Any suggestions? 有什么建议么?

Try adding Mockito to your sbt file: 尝试将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
)

Take care, it's a simple "%", not a double one (it's a java dependency) 请注意,它是一个简单的“%”,而不是双精度(它是一个java依赖项)

Other versions here : https://mvnrepository.com/artifact/org.mockito/mockito-all if 1.10.19 is not compatible with your codebase 此处的其他版本:如果1.10.19与您的代码库不兼容,则https://mvnrepository.com/artifact/org.mockito/mockito-all

EDIT : 编辑:

Not sure that will help your second problem, but try that in an SBT console : 不确定是否可以解决您的第二个问题,但是请在SBT控制台中尝试:

> clean
> compile

Versions <3.3.0 are not compatible with Scalatest 3+. <3.3.0版本与Scalatest 3+不兼容。

I suggest updating to scalamock 3.4.2 (latest one as of writing). 我建议更新为scalamock 3.4.2(撰写本文时为最新)。

You can always find the newest version on Maven Central. 您总是可以在Maven Central上找到最新版本。

Also, no need to specify scalamock-core, that gets pulled in automatically. 另外,无需指定自动导入的scalamock-core。

scalacheck 3.2 depend on scalatest 2.1.3, but you used scalatest 3.0.0, so they are not compatibal. scalacheck 3.2依赖于scalatest 2.1.3,但是您使用了scalatest 3.0.0,因此它们不兼容。 remove the scalatest dependence will resolve the problem. 消除最脆弱的依赖关系将解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM