mardi 2 août 2016

Mockito - How to restrict list type with list matchers?

I have the following interface:

public interface ListHandler {

    public boolean add(List<?> list, Object element);

}

For which I want to ensure in a test that it only receives a list with of a certain type, e.g., String. I have coded the following test:

@Test
public void testAddOnlyString() {
    boolean actual = false;

    when(this.listHandlerMock.add(
        Matchers.anyListOf(String.class),
        Matchers.anyString()
    )).thenReturn(true);

    actual = this.listHandlerMock.add(new ArrayList<Integer>(), 1);
    assertTrue(actual);

    verify(this.listHandlerMock).add(
        Matchers.anyListOf(String.class),
        Matchers.anyString()
    );
}

But the test passes correctly.

Aucun commentaire:

Enregistrer un commentaire