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

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

No matter what dependencies I set in my pom file I am not able to get the Mock to work. The compiler just says cannot resolve the symbol Mock and ClassOf. I have shown my dependencies below:

<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>

And the test dependency is:

<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>

These are my imports:

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

Any suggestions??

noMAD
  • 7,744
  • 19
  • 56
  • 94
  • Your example is very incomplete. I don't see the import for class MockitoJUnitRunner. Furthermore MockitoJUnitRunner is not even included in your dependencies. Maybe you want to add org.mockito:mockito-all:1.8.5 as dependency? And of course `mock` cannot be resolved, since you did not import any object that could provied it. – Stefan Endrullis Aug 10 '12 at 14:55
  • @StefanEndrullis: I have edited the post to show the imports. I also had the mockito dependency which I have now added to the post above. I hope this helps to make sense of it. I still can't get it to work. – noMAD Aug 10 '12 at 17:05

2 Answers2

3

I don't know mockito but according to

your class ScalaTest should extend a unit suite and should mix-in MockitoSugar (which provides the mock method).

If classOf cannot be resolved in your annotation declaration RunWith you probably have to use a newer Scala version (2.8 or better).

Community
  • 1
  • 1
Stefan Endrullis
  • 4,150
  • 2
  • 32
  • 45
0

You're importing the ScalaMock support classes for ScalaTest and JUnit, but you're not using ScalaTest at all (you're using JUnit with a custom runner, with which I'm not familiar) and seem to be using the wrong set of traits, as per Stefan's comment on yoru question.

If you want to use ScalaTest as your test runner, you should mix one of the relevant Suite classes along with MockitoSugar (follow the 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")
Tomer Gabel
  • 4,104
  • 1
  • 33
  • 37