Mockito verify Collection of String

b1zzu :

I want to verify that a exact Collection is passed to a mocked method. This is how I tried to do it:

This is a simple example of my real code, which reproduce exactly the same problem.

import com.google.common.collect.Lists;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.mockito.hamcrest.MockitoHamcrest;

import java.util.Collection;

public class MockTest {

    @Test
    void mockTest() {

        final Collection<String> collection = Mockito.mock(Collection.class);

        collection.addAll(Lists.newArrayList("hello"));

        Mockito.verify(collection).addAll(MockitoHamcrest.argThat(Matchers.contains("hello")));
    }
}

but it doesn't work and I get this compilation error:

Error:(20, 35) java: incompatible types: inference variable T has incompatible bounds
    equality constraints: java.lang.Iterable<? extends E>
    lower bounds: java.util.Collection<? extends java.lang.String>,java.lang.Object

Any idea on why it doesn't work or how can I do it differently?

Kumar Panchal :

can you please try below code

@Test
public void mockTest() {

    final Collection<String> collection = Mockito.mock(Collection.class);

    collection.addAll(Lists.newArrayList("hello"));

    Mockito.verify(collection).addAll((Collection<? extends String>) MockitoHamcrest.argThat(Matchers.contains("hello")));
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=103133&siteId=1