New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArgumentCaptor can't capture varargs-arrays #584

Open
ChristianSchwarz opened this Issue Aug 23, 2016 · 3 comments

Comments

4 participants
@ChristianSchwarz
Contributor

ChristianSchwarz commented Aug 23, 2016

Relates to: #439, #565, #567, #583

ArgumentCaptor can't capture varargs-arrays.

@Test
public void shouldCaptureVarArgsAsArray() throws Exception {
    ArgumentCaptor<String[]> varargCaptor = ArgumentCaptor.forClass(String[].class);

    mock.varargs("1","2");   

    verify(mock).varargs(varargCaptor.capture());

    assertThat(varargCaptor).containsExactly(new String[]{"1","2"});
}

Expected: A ArgumentCaptor<String[]> should be able to capture the varargs-array as a whole.
Actual: The ArgumentCaptor captures no arrays but every single argument passed to the varargs method. The following exception is thrown:

java.lang.AssertionError: 
Actual and expected should have same size but actual size was:
  <2>
while expected size was:
  <1>
Actual was:
  <["1", "2"]>
Expected was:
  <[["1", "2"]]>
@bric3

This comment has been minimized.

Contributor

bric3 commented Aug 23, 2016

For reference this issue is not new ; see #74, #201, #211

@paulduffin

This comment has been minimized.

Contributor

paulduffin commented Oct 26, 2017

Can you not just use varargCaptor.getAllValues() to access the list of arguments passed as varargs and then compare that?

@ChristianSchwarz

This comment has been minimized.

Contributor

ChristianSchwarz commented Oct 26, 2017

@paulduffin

Can you not just use varargCaptor.getAllValues() to access the list of arguments passed as varargs and then compare that?

This is not always possible cause getAllValues() returns all ever captured values.

when(mock).addVararg(captor.capture());
mock.addVararg(1,2);
mock.addVararg(3,4);
mock.addVararg(5,6);

captor.getAllValues() // {1,2,3,4,5,6}

vs.

when(mock).addVararg(captor.capture());
mock.addVararg(1,2,3);
mock.addVararg(4,5,6);

captor.getAllValues() // {1,2,3,4,5,6}

paulduffin added a commit to paulduffin/mockito that referenced this issue Oct 26, 2017

Fix issue mockito#1222 and mockito#584
Adds a new method varargsAsArray(...) that indicates that the varargs
should be matched/captured as a single array rather than separate
values.

@paulduffin paulduffin referenced a pull request that will close this issue Oct 26, 2017

Open

Fix issue #1222 and #584 #1224

paulduffin added a commit to paulduffin/mockito that referenced this issue Oct 26, 2017

Fix issue mockito#1222 and mockito#584
Adds a new method varargsAsArray(...) that indicates that the varargs
should be matched/captured as a single array rather than separate
values.

mockitoguy added a commit that referenced this issue Oct 31, 2017

Experimental flexible matching of varargs
Not intended for check-in but demonstrates that we can match varargs flexibly.

See #1224

Can we follow down this path and fix problem #1222 and #584 in the way the API is most convenient to use?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment