2017-06-13 53 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; 
    } 

}