0
@RunWith(classOf[MockitoJUnitRunner])
class ScalaTest {

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

无论我在我的 pom 文件中设置什么依赖项,我都无法让 Mock 工作。编译器只是说无法解析符号MockClassOf. 我在下面显示了我的依赖项:

<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

有什么建议么??

4

2 回答 2

3

我不知道 mockito 但根据

你的类ScalaTest应该扩展一个单元套件并且应该混入 MockitoSugar (它提供了mock方法)。

如果classOf无法在您的注释声明中解决,您RunWith可能必须使用更新的 Scala 版本(2.8 或更高版本)。

于 2012-08-13T12:41:12.777 回答
0

您正在为 ScalaTest 和 JUnit 导入 ScalaMock 支持类,但您根本没有使用 ScalaTest(您正在使用带有自定义运行器的 JUnit,我不熟悉)并且似乎使用了错误的集合特征,根据 Stefan 对 yoru 问题的评论。

如果您想使用 ScalaTest 作为您的测试运行器,您应该将相关套件类之一与 MockitoSugar 混合使用(遵循ScalaTest 文档):

// 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")
于 2012-10-24T13:49:28.493 回答