2012-08-08 53 views
0
@RunWith(classOf[MockitoJUnitRunner]) 
class ScalaTest { 

    var x = mock[java.util.Map] 
    val y = mock[ClassA] 
    val z = mock[ClassB] 
} 

無論我在我的pom文件中設置了什麼依賴關係,我都無法讓模擬工作。編譯器只是說無法解析符號MockClassOf。我已經表明我的依賴關係如下:在使用Scala時IntelliJ中沒有識別出模擬問題

<dependency> 
    <groupId>org.scalamock</groupId> 
    <artifactId>scalamock-scalatest-support_${scala.version}</artifactId> 
    <version>2.4</version> 
</dependency> 
<dependency> 
    <groupId>org.scalamock</groupId> 
    <artifactId>scalamock-junit3-support_${scala.version}</artifactId> 
    <version>2.4</version> 
</dependency> 

而且測試的依賴是:

<dependency> 
    <groupId>org.scalatest</groupId> 
    <artifactId>scalatest_${scala.version}</artifactId> 
    <version>2.0.M3</version> 
    <scope>test</scope> 
</dependency> 

<dependency> 
    <groupId>org.mockito</groupId> 
    <artifactId>mockito-core</artifactId> 
    <version>1.9.0</version> 
    <scope>test</scope> 
</dependency> 

這是我進口:

import org.junit.{Test, Before} 
import org.junit.runner.RunWith 
import org.mockito.runners.MockitoJUnitRunner 

任何建議?

+0

你舉的例子是非常不完整。我沒有看到類MockitoJUnitRunner的導入。此外MockitoJUnitRunner甚至不包含在你的依賴中。也許你想添加org.mockito:mockito-all:1.8.5作爲依賴項?當然'mock'不能解決,因爲你沒有導入任何可以提供它的對象。 – 2012-08-10 14:55:22

+0

@StefanEndrullis:我已編輯帖子以顯示導入。我也有我現在添加到上面的帖子mockito依賴項。我希望這有助於理解它。我仍然無法工作。 – noMAD 2012-08-10 17:05:58

回答

0

您正在導入ScalaTest和JUnit的ScalaMock支持類,但您根本沒有使用ScalaTest(您正在使用帶有自定義運行器的JUnit,我並不熟悉),並且似乎正在使用根據Stefan對yoru問題的評論,這是一組錯誤的特徵。

如果你想使用ScalaTest爲您的測試運行,您應該混合相關套件類之一,MockitoSugar沿(按照ScalaTest documentation):

// First, create the mock object 
val mockCollaborator = mock[Collaborator] 

// Create the class under test and pass the mock to it 
classUnderTest = new ClassUnderTest 
classUnderTest.addListener(mock) 

// Use the class under test 
classUnderTest.addDocument("Document", new Array[Byte](0)) 
classUnderTest.addDocument("Document", new Array[Byte](0)) 
classUnderTest.addDocument("Document", new Array[Byte](0)) 
classUnderTest.addDocument("Document", new Array[Byte](0)) 

// Then verify the class under test used the mock object as expected 
verify(mockCollaborator).documentAdded("Document") 
verify(mockCollaborator, times(3)).documentChanged("Document") 
相關問題