如何使用Mockito为不同的参数设置不同的返回类型?

时间:2017-06-13 11:41:20

标签: java mocking mockito

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

when(serviceInvocation.isServiceNeeded("4105706432","AAS")).thenReturn(true);

与“4105706432”类似,我只有4个其他字符串值必须返回值为true。 但是,对于任何其他字符串,我需要返回类型为false。

如何实施?

3 个答案:

答案 0 :(得分:1)

使用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);

答案 1 :(得分:0)

这将提供预期的行为:

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

答案 2 :(得分:0)

我会用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;
    }

}