Skip to content

Commit 2ce0b29

Browse files
author
Pankaj Kumar
committed
Mockito Mock Void Method Examples
1 parent aadf887 commit 2ce0b29

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
target/
2+
test-output/
23
.DS_Store
34
.classpath
45
.project
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.journaldev;
2+
3+
public class Employee {
4+
5+
private String name;
6+
7+
public String getName() {
8+
return name;
9+
}
10+
11+
public void setName(String name) {
12+
if (name == null)
13+
throw new IllegalArgumentException("Employee Name can't be null");
14+
this.name = name;
15+
}
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.journaldev.mockito.voidmethod;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
import static org.mockito.ArgumentMatchers.anyString;
7+
import static org.mockito.Mockito.doAnswer;
8+
import static org.mockito.Mockito.doThrow;
9+
import static org.mockito.Mockito.mock;
10+
import static org.mockito.Mockito.when;
11+
12+
import org.junit.jupiter.api.Test;
13+
14+
import com.journaldev.Employee;
15+
16+
class JUnitMockitoVoidMethod {
17+
18+
@Test
19+
void test_mockito_void() {
20+
Employee emp = mock(Employee.class);
21+
22+
doThrow(IllegalArgumentException.class).when(emp).setName(null);
23+
24+
doAnswer((i) -> {
25+
System.out.println("Employee setName Argument = " + i.getArgument(0));
26+
assertTrue("Pankaj".equals(i.getArgument(0)));
27+
return null;
28+
}).when(emp).setName(anyString());
29+
30+
when(emp.getName()).thenReturn("Pankaj");
31+
32+
assertThrows(IllegalArgumentException.class, () -> emp.setName(null));
33+
34+
emp.setName("Pankaj");
35+
assertEquals("Pankaj", emp.getName());
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.journaldev.mockito.voidmethod;
2+
3+
import static org.mockito.ArgumentMatchers.anyString;
4+
import static org.mockito.Mockito.doAnswer;
5+
import static org.mockito.Mockito.doThrow;
6+
import static org.mockito.Mockito.mock;
7+
import static org.mockito.Mockito.when;
8+
import static org.testng.Assert.assertEquals;
9+
import static org.testng.Assert.assertThrows;
10+
import static org.testng.Assert.assertTrue;
11+
12+
import org.testng.annotations.Test;
13+
14+
import com.journaldev.Employee;
15+
16+
class TestNGMockitoVoidMethod {
17+
18+
@Test
19+
void test_mockito_void() {
20+
Employee emp = mock(Employee.class);
21+
22+
doThrow(IllegalArgumentException.class).when(emp).setName(null);
23+
24+
doAnswer((i) -> {
25+
System.out.println("Employee setName Argument = " + i.getArgument(0));
26+
assertTrue("Pankaj".equals(i.getArgument(0)));
27+
return null;
28+
}).when(emp).setName(anyString());
29+
30+
when(emp.getName()).thenReturn("Pankaj");
31+
32+
assertThrows(IllegalArgumentException.class, () -> emp.setName(null));
33+
34+
emp.setName("Pankaj");
35+
assertEquals("Pankaj", emp.getName());
36+
}
37+
38+
}

0 commit comments

Comments
 (0)