Issue 310 in mockito: Misplaced argument matcher error interferes other test

3,031 views
Skip to first unread message

moc...@googlecode.com

unread,
Jan 15, 2012, 6:04:57 AM1/15/12
to mocki...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 310 by rfili...@gmail.com: Misplaced argument matcher error
interferes other test
http://code.google.com/p/mockito/issues/detail?id=310

What steps will reproduce the problem?

See SessionCountingListenerTest.java

1. Create a test method that makes use of any() incorrectly outside of
verification or stubbing.
2. Create a second test method that does nothing.
3. Run only the first test method within eclipse - the test passes, but
should fail.
4. Run the test class within eclipse - the second test fails, but should
not.

I am using Mockito 1.8.5 on Windows 7 64bit, but the problem can also be
observed when running the test class within Maven under Red Hat 64bit.

This is the way I reproduce it now. But in a previous state of the code
(which I cannot restore now_ we also observed that running the test class
within eclipse passed all tests. Running all test classes in the project
within eclipse or within Maven caused the error above.

Attachments:
SessionCountingListenerTest.java 1.3 KB
SessionCountingListener.java 2.0 KB
AppSharingCounter.java 2.4 KB
AppSharingCounterTest.java 3.5 KB

moc...@googlecode.com

unread,
Jan 15, 2012, 6:09:57 AM1/15/12
to mocki...@googlegroups.com

Comment #1 on issue 310 by rfili...@gmail.com: Misplaced argument matcher

Stack Trace from the second test that failed:

{code}
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Misplaced argument matcher detected here:
-> at
eu.lindenbaum.trafficdata.SessionCountingListenerTest.sessionOpenedEventIncrementsSessionState(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}

moc...@googlecode.com

unread,
Jan 15, 2012, 6:13:58 AM1/15/12
to mocki...@googlegroups.com

Comment #2 on issue 310 by rfili...@gmail.com: Misplaced argument matcher

JDK version: 1.6.0_25

moc...@googlecode.com

unread,
Jan 15, 2012, 6:18:59 AM1/15/12
to mocki...@googlegroups.com

Comment #3 on issue 310 by dmwallace.nz: Misplaced argument matcher error

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#validateMockitoUsage()

moc...@googlecode.com

unread,
Jan 15, 2012, 7:15:11 AM1/15/12
to mocki...@googlegroups.com

Comment #4 on issue 310 by rfili...@gmail.com: Misplaced argument matcher

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

moc...@googlecode.com

unread,
Jan 16, 2012, 3:41:23 AM1/16/12
to mocki...@googlegroups.com
Updates:
Status: Invalid

Comment #5 on issue 310 by brice.du...@gmail.com: Misplaced argument

(No comment was entered for this change.)

Reply all
Reply to author
Forward
0 new messages