Can I mix an Argument Captor and a regular helper?

I need to test a method with several parameters in Mockito, but I need to commit only one argument, and the other I need is just a simple helper. Is it possible?

For example, if I have:

@Mock
private Map<K,V> mockedMap;
...
ArgumentCaptor<K> argument = ArgumentCaptor.forClass(K.class);
verify(mockedMap).put(argument.capture(), any(V.class));

In this case, do I need to write a capture for each argument, even though I only need to write the first argument?

+4
source share
2 answers

In this case, do I need to write a capture for each argument, even though I only need to write the first argument?

durron597 answer - , . : ArgumentCaptor.capture() Mockito, Mockito, .

yourMock.yourMethod(int, int, int) ArgumentCaptor<Integer> intCaptor:

/*  good: */  verify(yourMock).yourMethod(2, 3, 4);  // eq by default
/*  same: */  verify(yourMock).yourMethod(eq(2), eq(3), eq(4));

/*   BAD: */  verify(yourMock).yourMethod(intCaptor.capture(), 3, 4);
/* fixed: */  verify(yourMock).yourMethod(intCaptor.capture(), eq(3), eq(4));

:

verify(yourMock).yourMethod(intCaptor.capture(), eq(5), otherIntCaptor.capture());
verify(yourMock).yourMethod(intCaptor.capture(), anyInt(), gt(9000));
+14

, . ?

import java.util.Map;

import org.junit.*;
import org.mockito.*;

import static org.mockito.Mockito.*;
import static org.junit.Assert.*;

public class MockitoTest {
  @Mock
  private Map<Integer, String> mockedMap;

  @Before
  public void setup() {
    MockitoAnnotations.initMocks(this);
  }

  @Test
  public void testCaptor() {
    mockedMap.put(5, "Hello World!");
    ArgumentCaptor<Integer> argument = ArgumentCaptor.forClass(Integer.class);
    verify(mockedMap).put(argument.capture(), any(String.class));

    assertEquals(5L, argument.getValue().longValue());
  }
}

.


, , List Map, , , , , , , . ( Mockito.spy), .

+2

Source: https://habr.com/ru/post/1608479/


All Articles