Skip to content

Commit 02c5310

Browse files
committed
1 parent de16319 commit 02c5310

File tree

2 files changed

+135
-135
lines changed

2 files changed

+135
-135
lines changed

Testing/Mockito/src/test/java/ListTest.java

Lines changed: 79 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
import static org.hamcrest.CoreMatchers.is;
1414
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;
1717
import static org.mockito.Mockito.when;
1818

1919
/**
@@ -39,81 +39,81 @@
3939
*/
4040

4141
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+
}
119119
}

Testing/Mockito/src/test/java/udemy/mociktoin28minutes/buisness/TodoBusinessImplMockitoTest.java

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import org.junit.Test;
44
import org.junit.runner.RunWith;
55
import org.mockito.*;
6-
import org.mockito.runners.MockitoJUnitRunner;
6+
import org.mockito.junit.MockitoJUnitRunner;
77
import udemy.mociktoin28minutes.data.api.TodoService;
88

99
import java.util.Arrays;
@@ -18,76 +18,76 @@
1818
@RunWith(MockitoJUnitRunner.class) //Instead of this we can use @Rule
1919
public class TodoBusinessImplMockitoTest {
2020

21-
@Mock
22-
private TodoService todoService;
23-
//TodoService todoService = = mock(TodoService.class); //GIVEN STEP
21+
@Mock
22+
private TodoService todoService;
23+
//TodoService todoService = = mock(TodoService.class); //GIVEN STEP
2424

25-
@InjectMocks
26-
private TodoBusinessImpl todoBusinessImpl;
27-
//TodoBusinessImpl todoBusinessImpl = new TodoBusinessImpl(todoService); //WHEN STEP
25+
@InjectMocks
26+
private TodoBusinessImpl todoBusinessImpl;
27+
//TodoBusinessImpl todoBusinessImpl = new TodoBusinessImpl(todoService); //WHEN STEP
2828

29-
@Captor
30-
private ArgumentCaptor<String> stringArgumentCaptor;
31-
//ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
29+
@Captor
30+
private ArgumentCaptor<String> stringArgumentCaptor;
31+
//ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
3232

33-
private List<String> allTodoTasks = Arrays.asList("Learn Spring MVC", "Learn Spring", "Learn to Dance");
33+
private List<String> allTodoTasks = Arrays.asList("Learn Spring MVC", "Learn Spring", "Learn to Dance");
3434

35-
@Test
36-
public void usingMockito() {
37-
//GIVEN
38-
//1. Mock
39-
//2. todos arglist
35+
@Test
36+
public void usingMockito() {
37+
//GIVEN
38+
//1. Mock
39+
//2. todos arglist
4040

41-
//WHEN
42-
when(todoService.retrieveTodoTasks("Saurabh")).thenReturn(allTodoTasks);
43-
List<String> todos = todoBusinessImpl.retrieveTodoTasksRelatedToSpring("Saurabh");
41+
//WHEN
42+
when(todoService.retrieveTodoTasks("Saurabh")).thenReturn(allTodoTasks);
43+
List<String> todos = todoBusinessImpl.retrieveTodoTasksRelatedToSpring("Saurabh");
4444

45-
//THEN
46-
assertEquals(2, todos.size());
47-
}
45+
//THEN
46+
assertEquals(2, todos.size());
47+
}
4848

49-
@Test
50-
public void usingMockito_UsingBDD() {
51-
//GIVEN
52-
//1. Mock
53-
//2. todos arglist
54-
BDDMockito.given(todoService.retrieveTodoTasks("Saurabh")).willReturn(allTodoTasks);
49+
@Test
50+
public void usingMockito_UsingBDD() {
51+
//GIVEN
52+
//1. Mock
53+
//2. todos arglist
54+
BDDMockito.given(todoService.retrieveTodoTasks("Saurabh")).willReturn(allTodoTasks);
5555

56-
//when
57-
List<String> todoTask = todoBusinessImpl.retrieveTodoTasksRelatedToSpring("Saurabh");
56+
//when
57+
List<String> todoTask = todoBusinessImpl.retrieveTodoTasksRelatedToSpring("Saurabh");
5858

59-
//then
60-
assertThat(todoTask.size(), is(2));
61-
}
59+
//then
60+
assertThat(todoTask.size(), is(2));
61+
}
6262

63-
@Test
64-
public void letsTestDeleteNow() {
65-
//GIVEN
66-
//1. Mock
67-
//2. todos arglist
63+
@Test
64+
public void letsTestDeleteNow() {
65+
//GIVEN
66+
//1. Mock
67+
//2. todos arglist
6868

69-
//WHEN
70-
when(todoService.retrieveTodoTasks("Saurabh")).thenReturn(allTodoTasks);
71-
todoBusinessImpl.deleteTodoTasksNotRelatedToSpring("Saurabh");
69+
//WHEN
70+
when(todoService.retrieveTodoTasks("Saurabh")).thenReturn(allTodoTasks);
71+
todoBusinessImpl.deleteTodoTasksNotRelatedToSpring("Saurabh");
7272

73-
//THEN
74-
verify(todoService).deleteTodo("Learn to Dance");
73+
//THEN
74+
verify(todoService).deleteTodo("Learn to Dance");
7575

76-
verify(todoService, Mockito.never()).deleteTodo("Learn Spring MVC");
76+
verify(todoService, Mockito.never()).deleteTodo("Learn Spring MVC");
7777

78-
verify(todoService, Mockito.never()).deleteTodo("Learn Spring");
78+
verify(todoService, Mockito.never()).deleteTodo("Learn Spring");
7979

80-
verify(todoService, Mockito.times(1)).deleteTodo("Learn to Dance"); // atLeastOnce, atLeast
81-
}
80+
verify(todoService, Mockito.times(1)).deleteTodo("Learn to Dance"); // atLeastOnce, atLeast
81+
}
8282

83-
@Test
84-
public void captureArgument() {
85-
//WHEN
86-
when(todoService.retrieveTodoTasks("Saurabh")).thenReturn(allTodoTasks);
87-
todoBusinessImpl.deleteTodoTasksNotRelatedToSpring("Saurabh");
83+
@Test
84+
public void captureArgument() {
85+
//WHEN
86+
when(todoService.retrieveTodoTasks("Saurabh")).thenReturn(allTodoTasks);
87+
todoBusinessImpl.deleteTodoTasksNotRelatedToSpring("Saurabh");
8888

89-
//THEN
90-
verify(todoService).deleteTodo(stringArgumentCaptor.capture());
91-
assertEquals("Learn to Dance", stringArgumentCaptor.getValue());
92-
}
89+
//THEN
90+
verify(todoService).deleteTodo(stringArgumentCaptor.capture());
91+
assertEquals("Learn to Dance", stringArgumentCaptor.getValue());
92+
}
9393
}

0 commit comments

Comments
 (0)