File tree 4 files changed +94
-0
lines changed
test/java/com/journaldev/mockito/voidmethod
4 files changed +94
-0
lines changed Original file line number Diff line number Diff line change 1
1
target /
2
+ test-output /
2
3
.DS_Store
3
4
.classpath
4
5
.project
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments