Skip to content

Commit

Permalink
Fixes issue mockito#1222
Browse files Browse the repository at this point in the history
  • Loading branch information
Alias-m committed Jul 28, 2018
1 parent 276183b commit 9d45694
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/main/java/org/mockito/internal/matchers/VarargMatcherImpl.java
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.matchers;

import org.mockito.ArgumentMatcher;

/**
* This class is used to verify with a vararg matcher
*/
public class VarargMatcherImpl<T> implements VarargMatcher, ArgumentMatcher<T> {
private T[] values;
private int iterator;
public VarargMatcherImpl(T... value) {
this.values = value;
}

@Override
public boolean matches(T argument){
iterator %= values.length;
return values[iterator++].equals(argument);
}

public String toString() {
String name = values.getClass().getComponentType().getSimpleName();
String str = "vararg<"+name+">(";
for(int i = 0 ; i < values.length; i++)
str += values[i] + (i < values.length - 1 ? ", " : ")");
return str;
}
}
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2017 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.matchers;

import org.junit.Test;
import org.mockito.ArgumentMatcher;
import org.mockitousage.IMethods;
import org.mockitoutil.TestBase;

import java.util.ArrayList;
import java.util.List;

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

/**
* This test is used to use vararg matcher instead of arrays
*/
public class VarargMatcherImplTest extends TestBase {

@Test
public void testMatcher() throws Exception {
ArgumentMatcher<String> matcher = new VarargMatcherImpl<String>("1", "2", "3");
assertTrue(matcher.matches("1"));
assertTrue(matcher.matches("2"));
assertTrue(matcher.matches("3"));
}

@Test
public void testMatcherString() throws Exception {
ArgumentMatcher<String> matcher = new VarargMatcherImpl<String>("1", "2", "3");
assertTrue(matcher.toString().equals("vararg<String>(1, 2, 3)"));
}

@Test
public void should_verify_varargs_as_array() throws Exception {
IMethods mock = mock(IMethods.class);
mock.mixedVarargs("1", "2", "3");
verify(mock).mixedVarargs(any(), argThat(new VarargMatcherImpl<String>("2", "3")));
}
}

0 comments on commit 9d45694

Please sign in to comment.