rahmandon / mockito

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

Mockito.verify threw InvalidUseOfMatchersException while it should have warned about final method #369

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Mockito cannot mock oracle.jbo.ViewCriteriaRow class.

Test case:
    @Test
    public void testSetAttribute() {
        ViewCriteriaRow row = Mockito.mock(ViewCriteriaRow.class);
        row.setAttribute("MyAttribute", new Object());
        Mockito.verify(row).setAttribute(Mockito.anyString(), Mockito.anyObject());
    }

Error returned:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
1 matchers expected, 2 recorded:
-> at view.MyTestClass.testSetAttribute(MyTestClass.java:84)
-> at view.MyTestClass.testSetAttribute(MyTestClass.java:84)

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 more info see javadoc for Matchers class.

     oracle.jbo.ViewCriteriaRow.setAttribute(ViewCriteriaRow.java:899)
     view.MyTestClass.testSetAttribute(MyTestClass.java:84)
     sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
     sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
     java.lang.reflect.Method.invoke(Method.java:597)
     org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
     org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
     org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
     org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
     org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
     org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
     org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
     org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
     org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
     org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
     org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
     org.junit.runners.ParentRunner.run(ParentRunner.java:220)

Original issue reported on code.google.com by discordi...@gmail.com on 27 Aug 2012 at 5:56

GoogleCodeExporter commented 8 years ago
Hi your snippet should work !

You should check there isn't any other matcher used somewhere else.

Also which version of Mockito are you using ?

Cheers
Brice

Original comment by brice.du...@gmail.com on 27 Aug 2012 at 9:49

GoogleCodeExporter commented 8 years ago
Yes, it should work and it works if I replace ViewCriteriaRow with, say, Row.
No other matchers.
If you look at the stack trace, you'll see that Mockito tries to invoke real 
setAttribute method from oracle.jbo.ViewCriteriaRow class. Is that normal?
I use Mockito 1.9.5-rc1, of course, sorry for not mentioning this.

Original comment by discordi...@gmail.com on 27 Aug 2012 at 1:51

GoogleCodeExporter commented 8 years ago
I took a look at the javadoc 
http://docs.oracle.com/cd/B13597_05/web.904/b10391/oracle/jbo/ViewCriteriaRow.ht
ml#setAttribute(java.lang.String,%20java.lang.Object). If this is the correct 
javadoc, your method is final, and it might cause problems with Mockito.

It's weird actually that the current error message doesn't state that the 
method is final! I think it should be reported while stubing, though!

Original comment by brice.du...@gmail.com on 27 Aug 2012 at 2:06

GoogleCodeExporter commented 8 years ago
Oh, you're right, it's final! Now when I know this I am able to mock it with 
PowerMock. Maybe this issue's title should be changed to reflect misguiding 
error message.

Original comment by discordi...@gmail.com on 28 Aug 2012 at 1:23

GoogleCodeExporter commented 8 years ago
Yes, will do, thanks for reporting the issue :)

Original comment by brice.du...@gmail.com on 3 Sep 2012 at 9:57

GoogleCodeExporter commented 8 years ago
Hi the message in 1.9.5-rc1 and trunk warns about the final methods, you should 
check that you really have mockito 1.9.5-rc1 in your classpath (there might be 
some maven tricks there).

Cheers,

Original comment by brice.du...@gmail.com on 6 Sep 2012 at 6:02

GoogleCodeExporter commented 8 years ago

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

GoogleCodeExporter commented 8 years ago

Original comment by szcze...@gmail.com on 16 Aug 2014 at 2:43

GoogleCodeExporter commented 8 years ago

Original comment by szcze...@gmail.com on 24 Aug 2014 at 3:14

GoogleCodeExporter commented 8 years ago

Original comment by szcze...@gmail.com on 24 Aug 2014 at 3:50