2017-06-13 52 views
2

我使用到的Mockito定义是这样的 -如何使用Mockito为不同的参数提供不同的返回类型?

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

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

这是如何实现的?

+0

使问题与您在示例中使用的方式相同,但使用其他字符串的问题有什么问题? –

+0

对于5个字符串,我必须确定返回类型为true。对于任何其他的,它一定是错误的。我无法指定每个其他字符串都是错误的。可能有这么多的字符串。 – user249117

+0

你试过这种方法吗? https://stackoverflow.com/a/30108950/4563745 –

回答

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); 
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 
+0

伙计们,为什么downvoting?这是正确的解决方案。 –

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; 
    } 

}