12
12
13
13
import static org .hamcrest .CoreMatchers .is ;
14
14
import static org .junit .Assert .*;
15
- import static org .mockito .Matchers .anyInt ;
16
- import static org .mockito .Matchers .eq ;
15
+ import static org .mockito .ArgumentMatchers .anyInt ;
16
+ import static org .mockito .ArgumentMatchers .eq ;
17
17
import static org .mockito .Mockito .when ;
18
18
19
19
/**
39
39
*/
40
40
41
41
public class ListTest {
42
- @ Rule
43
- public MockitoRule mockitoRule = MockitoJUnit .rule (); //Use this instead of RunWith(jUnitRunner)
44
-
45
- @ Mock
46
- private List <String > list ;
47
-
48
- @ Test
49
- public void letsMockListSize () {
50
- when (list .size ()).thenReturn (10 );
51
-
52
- //THEN
53
- assertEquals (10 , list .size ());
54
- }
55
-
56
- @ Test
57
- public void letsMockListSizeWithMultipleReturnValues () {
58
- when (list .size ()).thenReturn (10 ).thenReturn (20 );
59
-
60
- //THEN
61
- assertEquals (10 , list .size ()); // First Call
62
- assertEquals (20 , list .size ()); // Second Call
63
- }
64
-
65
- @ Test
66
- public void letsMockListGet () {
67
- when (list .get (0 )).thenReturn ("in28Minutes" );
68
-
69
- //THEN
70
- assertEquals ("in28Minutes" , list .get (0 ));
71
-
72
- assertNull (list .get (1 )); //if un-stubbed value is called
73
- }
74
-
75
- @ Test (expected = RuntimeException .class )
76
- public void letsMockListGetToThrowException () {
77
- when (list .get (Mockito .anyInt ())).thenThrow (new RuntimeException ("Something went wrong" ));
78
- list .get (0 );
79
- }
80
-
81
- @ Test
82
- public void letsMockListGetWithAny () {
83
- //Argument Matcher
84
- when (list .get (Mockito .anyInt ())).thenReturn ("in28Minutes" );
85
-
86
- //THEN
87
- assertEquals ("in28Minutes" , list .get (0 ));
88
- assertEquals ("in28Minutes" , list .get (1 ));
89
- }
90
-
91
- @ Test (expected = InvalidUseOfMatchersException .class )
92
- public void letsMockListGetWithMixedStubValues () {
93
- when (list .subList (anyInt (), 5 )).thenThrow (new RuntimeException ("Something went wrong" ));
94
- // If you are using argument matchers, all arguments have to be provided by matchers.
95
-
96
- //THEN
97
- assertEquals ("in28Minutes" , list .get (0 ));
98
- assertEquals ("in28Minutes" , list .get (1 ));
99
- }
100
-
101
- @ Test
102
- public void letsMockListGetWithMixedStubValuesButHamcrestWorkaround () {
103
- when (list .subList (anyInt (), eq (5 ))).thenReturn (Collections .singletonList ("Something" ));
104
-
105
- //THEN
106
- assertEquals ("Something" , list .subList (0 , 5 ).get (0 ));
107
- //assertEquals(null, list.subList(0,5).get(1)); Index out of bounds
108
- }
109
-
110
- @ Test
111
- public void bddAliases_UsingGivenWillReturn () {
112
- //given
113
- BDDMockito .given (list .get (Mockito .anyInt ())).willReturn ("in28Minutes" );
114
-
115
- //THEN
116
- assertThat ("in28Minutes" , is (list .get (0 )));
117
- assertThat ("in28Minutes" , is (list .get (1 )));
118
- }
42
+ @ Rule
43
+ public MockitoRule mockitoRule = MockitoJUnit .rule (); //Use this instead of RunWith(jUnitRunner)
44
+
45
+ @ Mock
46
+ private List <String > list ;
47
+
48
+ @ Test
49
+ public void letsMockListSize () {
50
+ when (list .size ()).thenReturn (10 );
51
+
52
+ //THEN
53
+ assertEquals (10 , list .size ());
54
+ }
55
+
56
+ @ Test
57
+ public void letsMockListSizeWithMultipleReturnValues () {
58
+ when (list .size ()).thenReturn (10 ).thenReturn (20 );
59
+
60
+ //THEN
61
+ assertEquals (10 , list .size ()); // First Call
62
+ assertEquals (20 , list .size ()); // Second Call
63
+ }
64
+
65
+ @ Test
66
+ public void letsMockListGet () {
67
+ when (list .get (0 )).thenReturn ("in28Minutes" );
68
+
69
+ //THEN
70
+ assertEquals ("in28Minutes" , list .get (0 ));
71
+
72
+ assertNull (list .get (1 )); //if un-stubbed value is called
73
+ }
74
+
75
+ @ Test (expected = RuntimeException .class )
76
+ public void letsMockListGetToThrowException () {
77
+ when (list .get (Mockito .anyInt ())).thenThrow (new RuntimeException ("Something went wrong" ));
78
+ list .get (0 );
79
+ }
80
+
81
+ @ Test
82
+ public void letsMockListGetWithAny () {
83
+ //Argument Matcher
84
+ when (list .get (Mockito .anyInt ())).thenReturn ("in28Minutes" );
85
+
86
+ //THEN
87
+ assertEquals ("in28Minutes" , list .get (0 ));
88
+ assertEquals ("in28Minutes" , list .get (1 ));
89
+ }
90
+
91
+ @ Test (expected = InvalidUseOfMatchersException .class )
92
+ public void letsMockListGetWithMixedStubValues () {
93
+ when (list .subList (anyInt (), 5 )).thenThrow (new RuntimeException ("Something went wrong" ));
94
+ // If you are using argument matchers, all arguments have to be provided by matchers.
95
+
96
+ //THEN
97
+ assertEquals ("in28Minutes" , list .get (0 ));
98
+ assertEquals ("in28Minutes" , list .get (1 ));
99
+ }
100
+
101
+ @ Test
102
+ public void letsMockListGetWithMixedStubValuesButHamcrestWorkaround () {
103
+ when (list .subList (anyInt (), eq (5 ))).thenReturn (Collections .singletonList ("Something" ));
104
+
105
+ //THEN
106
+ assertEquals ("Something" , list .subList (0 , 5 ).get (0 ));
107
+ //assertEquals(null, list.subList(0,5).get(1)); Index out of bounds
108
+ }
109
+
110
+ @ Test
111
+ public void bddAliases_UsingGivenWillReturn () {
112
+ //given
113
+ BDDMockito .given (list .get (Mockito .anyInt ())).willReturn ("in28Minutes" );
114
+
115
+ //THEN
116
+ assertThat ("in28Minutes" , is (list .get (0 )));
117
+ assertThat ("in28Minutes" , is (list .get (1 )));
118
+ }
119
119
}
0 commit comments