Verify using isA(Class<T> clazz)

295 views
Skip to first unread message

Eduardo Martins

unread,
Jul 21, 2014, 5:48:13 PM7/21/14
to moc...@googlegroups.com
I'm trying to use Mockito to verify if a method has been called at my test class. 
Something like this:

public class FooTest {

    // Bar contains a method who receives a 
   // Dummy class like  doSomething(Dummy d, String abc);

    @Test
    public void testFoo() {
        Bar bar = Mockito.mock(Bar.class);
        Foo foo = new Foo(bar);
        foo.doMagic("anyParameter");
        Mockito.verify(bar).doSomething(isA(Dummy.class), anyString());
    }

}

But when I call the verify method I got this error:

"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."

May I use verify with any values? I also tried using equals on Dummy class but I got an error too,
as Mockito looks for the object reference instead of object values.

How can I assure that this method was called?

Thanks

Brice Dutheil

unread,
Jul 22, 2014, 4:17:25 AM7/22/14
to moc...@googlegroups.com

The snippet looks good. Did you verified the import of the matchers matched org.mockito.Matchers.* ?

Cheers,

— Brice

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at http://groups.google.com/group/mockito.
For more options, visit https://groups.google.com/d/optout.

Eduardo Martins

unread,
Jul 22, 2014, 9:13:52 AM7/22/14
to moc...@googlegroups.com
Hi Brice, thanks for the answer. I already imported:

import static org.mockito.Matchers.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

So, is it a bug?


On Tuesday, July 22, 2014 5:17:25 AM UTC-3, Brice wrote:

The snippet looks good. Did you verified the import of the matchers matched org.mockito.Matchers.* ?

Cheers,

— Brice

Brice Dutheil

unread,
Jul 22, 2014, 10:20:45 AM7/22/14
to moc...@googlegroups.com
Probably not. If you run the code snippet I cannot reproduce this behavior. So there's something in the code you' re trying to mack verify that reaches the current limitation of mockito.
Is your method final ? Is the parent of Bar not public, and does the `doSomething` method declared in that parent?

-- Brice

Marcin Zajączkowski

unread,
Jul 22, 2014, 10:29:08 AM7/22/14
to moc...@googlegroups.com
On 2014-07-22 16:20, Brice Dutheil wrote:
> Probably not. If you run the code snippet I cannot reproduce this behavior.
> So there's something in the code you' re trying to mack verify that reaches
> the current limitation of mockito.
> Is your method final ? Is the parent of Bar not public, and does
> the `doSomething` method declared in that parent?

Eduardo, you can also generate a small project which reproduces that
problem.

Marcin


> On Tue, Jul 22, 2014 at 3:13 PM, Eduardo Martins <
> eduardo...@luizalabs.com> wrote:
>
>> Hi Brice, thanks for the answer. I already imported:
>>
>> import static org.mockito.Matchers.*;
>> import static org.mockito.Mockito.mock;
>> import static org.mockito.Mockito.verify;
>>
>> So, is it a bug?
>>
>>
>> On Tuesday, July 22, 2014 5:17:25 AM UTC-3, Brice wrote:
>>
>>> The snippet looks good. Did you verified the import of the matchers
>>> matched org.mockito.Matchers.* ?
>>>
>>> Cheers,
>>>
>>> — Brice
>>>
>>> On Mon, Jul 21, 2014 at 11:48 PM, Eduardo Martins
>>> [eduardo...@luizalabs.com]
>>> <http://mailto:%5Beduard...@luizalabs.com%5D(mailto:eduardo...@luizalabs.com)>
--
http://blog.solidsoft.info/ - Working code is not enough

Eduardo Martins

unread,
Jul 22, 2014, 10:50:31 AM7/22/14
to moc...@googlegroups.com
Yes, i will post the code on gist.


On Tuesday, July 22, 2014 11:29:08 AM UTC-3, Marcin Zajączkowski wrote:
On 2014-07-22 16:20, Brice Dutheil wrote:
> Probably not. If you run the code snippet I cannot reproduce this behavior.
> So there's something in the code you' re trying to mack verify that reaches
> the current limitation of mockito.
> Is your method final ? Is the parent of Bar not public, and does
> the `doSomething` method declared in that parent?

Eduardo, you can also generate a small project which reproduces that
problem.

Marcin


> On Tue, Jul 22, 2014 at 3:13 PM, Eduardo Martins <
> eduardo...@luizalabs.com> wrote:
>
>> Hi Brice, thanks for the answer. I already imported:
>>
>> import static org.mockito.Matchers.*;
>> import static org.mockito.Mockito.mock;
>> import static org.mockito.Mockito.verify;
>>
>> So, is it a bug?
>>
>>
>> On Tuesday, July 22, 2014 5:17:25 AM UTC-3, Brice wrote:
>>
>>> The snippet looks good. Did you verified the import of the matchers
>>> matched org.mockito.Matchers.* ?
>>>
>>> Cheers,
>>>
>>> — Brice
>>>
>>> On Mon, Jul 21, 2014 at 11:48 PM, Eduardo Martins
>>> [eduardo...@luizalabs.com]

Eduardo Martins

unread,
Jul 22, 2014, 11:09:14 AM7/22/14
to moc...@googlegroups.com
Marcin, i created a project with the problem:

Brice Dutheil

unread,
Jul 22, 2014, 11:55:08 AM7/22/14
to moc...@googlegroups.com

There’s the error in the snippet in the github project (not in the snippet of the first mail ;))

Bar bar = Mockito.mock(Bar.class);
Foo foo = new Foo(bar); 
foo.doMagic(anyString());
Mockito.verify(bar).doSomething(isA(Dummy.class), anyString());

the following line is incorrect :

foo.doMagic(anyString());

You cannot use a matcher there. A mockito matcher need to be used either when you stub or when you verify.
Instead you should use a concrete value when invoking the object under test. The following snippet is green :

Bar bar = Mockito.mock(Bar.class);
Foo foo = new Foo(bar);
foo.doMagic("whatever");
Mockito.verify(bar).doSomething(isA(Dummy.class), anyString());

HTH

Cheers,

— Brice

--

Eduardo Martins

unread,
Jul 22, 2014, 1:21:59 PM7/22/14
to moc...@googlegroups.com
Now this works on my dummy project!

But on my real project is a test on a jersey rest service and i can't solve...

I tried to make this error on my dummy project, but it's worked:

https://github.com/pecota/verifyTest

I think i'm making some stupid mistake (on my real code) like 
foo.doMagic(anyString());
Now I know mockito works, i will find my error. 

Thanks again!


On Tuesday, July 22, 2014 12:55:08 PM UTC-3, Brice wrote:

There’s the error in the snippet in the github project (not in the snippet of the first mail ;))

Bar bar = Mockito.mock(Bar.class);
Foo foo = new Foo(bar); 
foo.doMagic(anyString());
Mockito.verify(bar).doSomething(isA(Dummy.class), anyString());

the following line is incorrect :

foo.doMagic(anyString());

You cannot use a matcher there. A mockito matcher need to be used either when you stub or when you verify.
Instead you should use a concrete value when invoking the object under test. The following snippet is green :

Bar bar = Mockito.mock(Bar.class);
Foo foo = new Foo(bar);
foo.doMagic("whatever");
Mockito.verify(bar).doSomething(isA(Dummy.class), anyString());

HTH

Cheers,

— Brice

Eduardo Martins

unread,
Jul 22, 2014, 1:57:45 PM7/22/14
to moc...@googlegroups.com
Brice, I found my error!

I was mixing matchers with real objects. I pushed a test case exemplifying my error:

https://github.com/eduardomartins-luizalabs/verifyTest

When I do:

    @Test
    public void testFoo() {
        Form form = new Form();
        form.param("name", "eduardo");
        form.param("id", "123");

        Response response = target("/service/dummy").request().post(Entity.form(form));

        ArgumentCaptor<Dummy> captor = ArgumentCaptor.forClass(Dummy.class);
        Mockito.verify(bar).doSomething(captor.capture(), "service");

        Assert.assertEquals(HttpStatus.OK_200.getStatusCode(), response.getStatus());
        Assert.assertEquals("eduardo", captor.getValue().getName());
    }


I got this:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
2 matchers expected, 1 recorded:
-> at FooServiceTest.testFoo(FooServiceTest.java:47)

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

But when I do this:

    @Test
    public void testFoo() {
        Form form = new Form();
        form.param("name", "eduardo");
        form.param("id", "123");

        Response response = target("/service/dummy").request().post(Entity.form(form));

        ArgumentCaptor<Dummy> captor = ArgumentCaptor.forClass(Dummy.class);
        Mockito.verify(bar).doSomething(captor.capture(), anyString());

        Assert.assertEquals(HttpStatus.OK_200.getStatusCode(), response.getStatus());
        Assert.assertEquals("eduardo", captor.getValue().getName());
    }

Works fine!

I had to use the org.mockito.Matchers.eq !

Simple mistake... sorry causing trouble.

Thanks!

Eduardo Martins

unread,
Jul 22, 2014, 2:08:05 PM7/22/14
to moc...@googlegroups.com
Closed question!

Thanks!

Brice Dutheil

unread,
Jul 22, 2014, 3:11:23 PM7/22/14
to moc...@googlegroups.com
Ok :)

Thanks for the feedback :)

And happy mocking!

-- Brice


On Tue, Jul 22, 2014 at 8:08 PM, Eduardo Martins <eduardo...@luizalabs.com> wrote:
Closed question!
Reply all
Reply to author
Forward
0 new messages