1
+ package com .baeldung .genericlist ;
2
+
3
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4
+ import static org .mockito .ArgumentMatchers .any ;
5
+ import static org .mockito .ArgumentMatchers .anyList ;
6
+ import static org .mockito .Mockito .when ;
7
+
8
+ import java .util .ArrayList ;
9
+ import java .util .List ;
10
+
11
+ import org .junit .jupiter .api .Test ;
12
+ import org .mockito .ArgumentMatchers ;
13
+ import org .mockito .Mockito ;
14
+
15
+ interface MyInterface {
16
+
17
+ default String extractFirstLetters (List <String > words ) {
18
+ return String .join (", " , words .stream ()
19
+ .map (str -> str .substring (0 , 1 ))
20
+ .toList ());
21
+ }
22
+ }
23
+
24
+ public class MatchGenericListUnitTest {
25
+
26
+ @ Test
27
+ void whenUseAnyWithListClass_thenCorrect () {
28
+ MyInterface mock = Mockito .mock (MyInterface .class );
29
+ when (mock .extractFirstLetters (any (List .class ))).thenReturn ("a, b, c, d, e" );
30
+ assertEquals ("a, b, c, d, e" , mock .extractFirstLetters (new ArrayList <>()));
31
+ }
32
+
33
+ @ Test
34
+ void whenUseAnyListWithTypeParameterInJava7_thenCorrect () {
35
+ //Java 7
36
+ MyInterface mock = Mockito .mock (MyInterface .class );
37
+ when (mock .extractFirstLetters (ArgumentMatchers .<String > anyList ())).thenReturn ("a, b, c, d, e" );
38
+ assertEquals ("a, b, c, d, e" , mock .extractFirstLetters (new ArrayList <>()));
39
+ }
40
+
41
+ @ Test
42
+ void whenUseAnyList_thenCorrect () {
43
+ MyInterface mock = Mockito .mock (MyInterface .class );
44
+ when (mock .extractFirstLetters (anyList ())).thenReturn ("a, b, c, d, e" );
45
+ assertEquals ("a, b, c, d, e" , mock .extractFirstLetters (new ArrayList <>()));
46
+ }
47
+
48
+ @ Test
49
+ void whenUseAny_thenCorrect () {
50
+ MyInterface mock = Mockito .mock (MyInterface .class );
51
+ when (mock .extractFirstLetters (any ())).thenReturn ("a, b, c, d, e" );
52
+ assertEquals ("a, b, c, d, e" , mock .extractFirstLetters (new ArrayList <>()));
53
+ }
54
+
55
+ }
0 commit comments