dhamini-poornachandra / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

Misplaced argument matcher error interferes other test #310

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
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.

Original issue reported on code.google.com by rfili...@gmail.com on 15 Jan 2012 at 11:04

Attachments:

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

GoogleCodeExporter commented 8 years ago
JDK version: 1.6.0_25

Original comment by rfili...@gmail.com on 15 Jan 2012 at 11:10

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago

Original comment by brice.du...@gmail.com on 16 Jan 2012 at 8:40

GoogleCodeExporter commented 8 years ago

Original comment by brice.du...@gmail.com on 2 Dec 2012 at 10:21