Closed GoogleCodeExporter closed 8 years ago
Stack Trace from the second test that failed:
{code}
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Misplaced argument matcher detected here:
-> at
eu.lindenbaum.trafficdata.SessionCountingListenerTest.sessionOpenedEventIncremen
tsSessionState(SessionCountingListenerTest.java:36)
You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
when(mock.get(anyInt())).thenReturn(null);
doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
verify(mock).someMethod(contains("foo"))
Also, this error might show up because you use argument matchers with methods
that cannot be mocked.
Following methods *cannot* be stubbed/verified:
final/private/equals()/hashCode().
at eu.lindenbaum.trafficdata.SessionCountingListenerTest.setUp(SessionCountingListenerTest.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
{code}
Original comment by rfili...@gmail.com
on 15 Jan 2012 at 11:09
JDK version: 1.6.0_25
Original comment by rfili...@gmail.com
on 15 Jan 2012 at 11:10
Yes, this is because when any() is misused, there's no way for Mockito to tell,
until the next time a call to a Mockito method occurs. There are a couple of
ways around this - you might want to take a look at
http://docs.mockito.googlecode.com/hg/latest/org/mockito/Mockito.html#validateMo
ckitoUsage()
Original comment by dmwallace.nz
on 15 Jan 2012 at 11:18
I see your point, it is not possible for Mockito to check if it is used
correctly, because the check can only be triggered by a method call towards
Mockito in the test.
I used the Mockito runner which does this check automatically and it fixed the
issue.
@RunWith(MockitoJUnitRunner.class)
public class SessionCountingListenerTest
Since we are just starting to use Mockito and have not so much experience with
it yet, I would use it so that we detect such errors early. Perhaps it would be
good to recommend it in the docs.
Thanks for the hint ;)
Original comment by rfili...@gmail.com
on 15 Jan 2012 at 12:15
Original comment by brice.du...@gmail.com
on 16 Jan 2012 at 8:40
Original comment by brice.du...@gmail.com
on 2 Dec 2012 at 10:21
Original issue reported on code.google.com by
rfili...@gmail.com
on 15 Jan 2012 at 11:04Attachments: