Hamcrest hasItem and Mockito verify inconsistency
Asked Answered
T

2

0

I've ran into an issue with hamcrest and mockito. Here is what I'm trying to do:

public class A{
     public void foo(List<B> arg){
          return;
     }
}

public BMatcher extends BaseMatcher<B>{
 //Some impl...
}

In my test I want to do something like

A a = mock(A.class);
B expected = new B();
Mockito.verify(a).foo(argThat(JUnitMatchers.hasItem(new BMatcher(expected)));

However, the hasItem matcher returns an Iterable<B> while the foo method expects a List<B>. Is there any good way of verifying the method is called properly?

Tormentil answered 18/6, 2013 at 16:30 Comment(0)
G
1

You could use an ArgumentCaptor.

 @Captor
 ArgumentCaptor<List<B>> captor;

 // then in test
 ...
 verify(a).foo(captor.capture());
 List<B> values = captor.getValue();
 assertThat(values, IsIterableContainingInOrder.containingInOrder(new BMatcher(expected));
 ...

I used the @Captor as a shortcut and also to that it could be a List<B> instead of just List. This requires the use of MockitoAnnotations.init(this) in an @Before method.

Gust answered 18/6, 2013 at 16:46 Comment(0)
S
1

You can use the 'argThat' method in the Mockito Matchers class to convert from a hamcrest Matcher to a mockito argument matcher:

import static org.mockito.Matchers.argThat;
import org.hamcrest.Matchers;
import org.hamcrest.Matcher;
//....
public static <T> List<T> listWithItem(Matcher<T> m)
{
    return (List<T>)argThat(Matchers.hasItem(m));
}
Serotherapy answered 23/5, 2016 at 10:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.