Java 如何使用Mockito为不同的参数提供不同的返回类型?

Java 如何使用Mockito为不同的参数提供不同的返回类型?,java,mocking,mockito,Java,Mocking,Mockito,我用Mockito来定义这样的东西- when(serviceInvocation.isServiceNeeded("4105706432","AAS")).thenReturn(true); 与“4105706432”类似,我只有4个其他字符串值,它们的返回值必须为true。 但是,对于任何其他字符串,我需要返回类型为false 如何实现这一点?使用ArgumentCaptor捕获参数:然后您可以检查它是否是正确的值之一: ArgumentCaptor<String> capto

我用Mockito来定义这样的东西-

when(serviceInvocation.isServiceNeeded("4105706432","AAS")).thenReturn(true);
与“4105706432”类似,我只有4个其他字符串值,它们的返回值必须为true。 但是,对于任何其他字符串,我需要返回类型为false


如何实现这一点?

使用ArgumentCaptor捕获参数:然后您可以检查它是否是正确的值之一:

ArgumentCaptor<String> captor= ArgumentCaptor.forClass(String.class);
Mockito.verify(mock).doSomething(captor.capture(), String));

String argument = captor.getValue()
//suppose there is a list with valid Strings
validList.contains(argument);
ArgumentCaptor captor=ArgumentCaptor.forClass(String.class);
Mockito.verify(mock).doSomething(captor.capture(),String));
字符串参数=captor.getValue()
//假设有一个包含有效字符串的列表
有效列表。包含(参数);

这将提供预期的行为:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class TestClass {

    @Mock
    private ServiceInvocation serviceInvocation;


    @Test
    public void testMethod() {
        when(serviceInvocation.isServiceNeeded(anyString(),anyString())).thenReturn(false);
        when(serviceInvocation.isServiceNeeded("1","AAS")).thenReturn(true);
        when(serviceInvocation.isServiceNeeded("2","AAS")).thenReturn(true);
        when(serviceInvocation.isServiceNeeded("3","AAS")).thenReturn(true);
        when(serviceInvocation.isServiceNeeded("4","AAS")).thenReturn(true);
        when(serviceInvocation.isServiceNeeded("5","AAS")).thenReturn(true);

        System.out.println(serviceInvocation.isServiceNeeded("1","AAS"));
        System.out.println(serviceInvocation.isServiceNeeded("2","AAS"));
        System.out.println(serviceInvocation.isServiceNeeded("3","AAS"));
        System.out.println(serviceInvocation.isServiceNeeded("4","AAS"));
        System.out.println(serviceInvocation.isServiceNeeded("5","AAS"));
        System.out.println(serviceInvocation.isServiceNeeded("other1","AAS"));
        System.out.println(serviceInvocation.isServiceNeeded("other2","AAS"));
        System.out.println(serviceInvocation.isServiceNeeded("3","AAS"));
    }
}
结果:

true
true
true
true
true
false
false

我会用ArgumentMatcher解决这个问题,如下所示:

import org.junit.Test;
import org.mockito.ArgumentMatcher;

import java.util.Arrays;
import java.util.List;

import static org.mockito.Mockito.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * <dependency>
 * <groupId>org.mockito</groupId>
 * <artifactId>mockito-core</artifactId>
 * <version>2.8.9</version>
 * <scope>test</scope>
 * </dependency>
 */
public class MockitoExample1 {


    @Test
    public void test1() {


        MyClass test = mock(MyClass.class);


        String sampleText = argThat(new MyMatcher());

        when(test.isServiceNeeded(sampleText)).thenReturn(true);

        boolean reply;
        reply = test.isServiceNeeded("0000000005");
        System.out.println(reply);  // true

        reply = test.isServiceNeeded("000000000x");
        System.out.println(reply);  // false

    }


}

class MyMatcher extends ArgumentMatcher<String> {

    @Override
    public boolean matches(Object argument) {
        List<String> validStrings = Arrays.asList("0000000001", "0000000002", "0000000003", "0000000004", "0000000005");
        if (validStrings.contains((String) argument)) {
            return true;
        }
        return false;
    }
}

class MyClass {
    public boolean isServiceNeeded(String text) {
        return true;
    }

}
import org.junit.Test;
导入org.mockito.ArgumentMatcher;
导入java.util.array;
导入java.util.List;
导入静态org.mockito.mockito.argThat;
导入静态org.mockito.mockito.mock;
导入静态org.mockito.mockito.when;
/**
* 
*org.mockito
*莫基托磁芯
* 2.8.9
*试验
* 
*/
公共类模拟示例1{
@试验
公共void test1(){
MyClass测试=模拟(MyClass.class);
String sampleText=argThat(新的MyMatcher());
当(test.isServiceNeeded(sampleText)),然后返回(true);
布尔回复;
回复=测试。需要服务(“0000000005”);
System.out.println(reply);//true
回复=测试。需要服务(“000000000 x”);
System.out.println(reply);//false
}
}
类MyMatcher扩展ArgumentMatcher{
@凌驾
公共布尔匹配(对象参数){
List validstring=Arrays.asList(“0000000001”、“0000000002”、“0000000003”、“0000000004”、“0000000005”);
if(validStrings.contains((字符串)参数)){
返回true;
}
返回false;
}
}
类MyClass{
公共布尔值isServiceNeeded(字符串文本){
返回true;
}
}

与您在示例中使用的方法相同,但使用其他字符串有什么问题?对于5个字符串,已修复了返回类型必须为true的问题。对于任何其他人来说,这肯定是错误的。我不能指定其他所有将为false的字符串。可能有这么多字符串。您尝试过这种方法吗?Thanx为源代码。但是这种方法对5个字符串有效吗。我的意思是这个方法只提到了一个字符串。对不起,如果我的问题很愚蠢。