2016-11-14 77 views
2

我是scalatest和scalamock的新手。下面是我在我的SBT文件scalatest的ScalaMock

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. 

有什麼建議?

回答

5

嘗試增加的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 0.19是不是與你的代碼兼容

編輯:

不知道,這將有助於你的第二個問題,但嘗試在SBT控制檯:

> clean 
> compile 
+0

是基於mockito頂部的scalamock? –

+0

請參閱編輯問題 –

+0

不,scalamock和mockito是兩個不同的項目。我不明白你在測試用例中包含了這兩個庫。我會說刪除您的測試用例中的「import org.scalatest.mockito.MockitoSugar._」行,並在您的「org.mockito」%「mockito-all」%「1.10.19」%Test「行中刪除build.sbt文件,然後重試。請參閱該頁面:http://scalamock.org/user-guide/integration/ –

0

scalacheck 3.2依賴於scalatest 2.1.3,但你使用了scalatest 3.0.0,所以它們不是兼容的。刪除scalatest依賴將解決問題。

1

版本< 3.3.0與Scalatest 3+不兼容。

我建議更新到scalamock 3.4.2(寫作時最新的一個)。

您可以隨時在Maven Central上找到最新版本。

另外,不需要指定自動拉入的scalamock-core。