2016-04-21 44 views
0
比較

我的方法接口- 使用的Mockito的一個值從值列表中匹配

Boolean isAuthenticated(String User) 

我想如果任何用戶都在功能從列表中通過從值列表進行比較,那麼它應該返回true。

when(authService.isAuthenticated(or(eq("amol84"),eq("arpan"),eq("juhi")))).thenReturn(true); 

我正在使用額外的參數匹配器'或',但上面的代碼不起作用。我該如何解決這個問題?

回答

1

or沒有三參數重載。 (See docs.)如果您的代碼編譯完成,則可能會導入與org.mockito.AdditionalMatchers.or不同的or方法。

or(or(eq("amol84"),eq("arpan")),eq("juhi"))應該工作。

您也可以嘗試isOneOf Hamcrest matcher,通過argThat Mockito matcher訪問:

when(authService.isAuthenticated(argThat(isOneOf("amol84", "arpan", "juhi")))) 
    .thenReturn(true); 
0

您可以定義不同的答案:

when(authService.isAuthenticated(eq("amol84"))).thenReturn(true); 
when(authService.isAuthenticated(eq("arpan"))).thenReturn(true); 
when(authService.isAuthenticated(eq("juhi"))).thenReturn(true); 
0

對於我這樣工作的:

public class MockitoTest { 

    Mocked mocked = Mockito.mock(Mocked.class); 

    @Test 
    public void test() { 
     Mockito.when(mocked.doit(AdditionalMatchers.or(eq("1"), eq("2")))).thenReturn(true); 

     Assert.assertTrue(mocked.doit("1")); 
     Assert.assertTrue(mocked.doit("2")); 
     Assert.assertFalse(mocked.doit("3")); 
    } 
} 

interface Mocked { 
    boolean doit(String a); 
} 

請檢查您是否正確設置的Mockito,或者如果你使用相同的匹配器,因爲我做。

相關問題