Reputation: 700
I am getting an unexpected error when trying to perform a simple test with mocks.
@RunWith(MockitoJUnitRunner)
class AccessorTest {
@Mock
private DeviceBuilder deviceBuilder
@Test
void shouldCreateDeviceFromFilesystem() {
//given
URI uri = this.class.classLoader.getResource("sample-filesystem").toURI()
File deviceRoot = new File(uri)
Accessor accessor = new Accessor(deviceBuilder)
Device expectedDevice = new Device(deviceRoot)
when(deviceBuilder.build(eq(deviceRoot))).thenReturn(expectedDevice)
//when
Device device = accessor.readFrom(deviceRoot)
//then
assert device == expectedDevice
verify(deviceBuilder).build(deviceRoot)
}
}
The DeviceBuilder is a single method interface Device::DeviceBuilder#build(File root). Device has a well defined equals method as per Josh Bloch.
The exception is thrown on the when() line, and none of the variables in scope are null. The full exception is:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
0 matchers expected, 1 recorded.
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
For what it's worth here's a snippet of my POM:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.7.0-01</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.5</version>
<scope>test</scope>
</dependency>
</dependencies>
My suspicions are either some weirdness from the way Groovy manages classes, or some version incompatibility, but I hope it's something obvious I just can't see.
Upvotes: 3
Views: 1159
Reputation: 3210
I'm not answering the original question.
I've searched everywhere and the only question about the error I got points to this discussion.
In case if someone got this error: do not call mock methods to provide values for matchers. The call deviceBuilder.build
should go BEFORE verify(
.
Upvotes: -1
Reputation: 694
I know this is an old question, but this may be helpful to someone else.
The issue described in the question is detailed at http://code.google.com/p/mockito/issues/detail?id=303
I ran into the very same issue and I got it fixed by using the library provided at https://github.com/cyrusinnovation/mockito-groovy-support
Upvotes: 2
Reputation: 10262
Sometimes Mockito doesn't flag inappropriate uses at the time it occurrs but at the next invocation. You could have a look at the test before that one, maybe you have a problem there?
The message says that in an invocation you either have to provide only Matchers or only real objects, but not a combination thereof. You can usually fix that by using eq(obj) instead of the object itself (or sameInstance(obj)), to have all Matchers. However in this case I don't find any place in the code you posted that would fit that description, that's why I suspect a problem in an earlier place of the code.
Upvotes: 0