2017-08-01 19 views
0

次のメソッドを擬似したいと思います。しかし、Java.util.Functionを使用する2番目のパラメータにはMockito.Matchersが見つかりません。Mockito/PowerMockito - ラムダ式をパラメータとして受け取るメソッドをモックします

public List<String> convertStringtoInt(List<Integer> intList,Function<Integer, String> intToStringExpression) { 
     return intList.stream() 
       .map(intToStringExpression) 
       .collect(Collectors.toList()); 
    } 

私はこのような何かを探しています:あなたが唯一、次のいずれかが動作しますその後、関数の引数をモックとしたい場合は

Mockito.when(convertStringtoInt(Matchers.anyList(),Matchers.anyFunction()).thenReturn(myMockedList) 

答えて

2

:クラスを考えると

Mockito.when(convertStringtoInt(Matchers.anyList(), Mockito.any(Function.class))).thenReturn(myMockedList); 

Mockito.when(convertStringtoInt(Matchers.anyList(), Mockito.<Function>anyObject())).thenReturn(myMockedList); 

Foo、これは方法を含む:public List<String> convertStringtoInt(List<Integer> intList,Function<Integer, String> intToStringExpression)次のテストケースはパスする:

@Test 
public void test_withMatcher() { 
    Foo foo = Mockito.mock(Foo.class); 

    List<String> myMockedList = Lists.newArrayList("a", "b", "c"); 

    Mockito.when(foo.convertStringtoInt(Matchers.anyList(), Mockito.<Function>anyObject())).thenReturn(myMockedList); 

    List<String> actual = foo.convertStringtoInt(Lists.newArrayList(1), new Function<Integer, String>() { 
     @Override 
     public String apply(Integer integer) { 
      return null; 
     } 
    }); 

    assertEquals(myMockedList, actual); 
} 

注:実際にFunctionパラメータの動作を呼び出して制御する場合は、thenAnswer()を参照する必要があります。

+0

@Glictch。これは完全に機能します。ブリリアント! 'Mockito.when(convertStringtoInt(Matchers.anyList()、Mockito。 anyObject()))。then return(myMockedList);' – naga1990

関連する問題