File tree 2 files changed +61
-0
lines changed
testing-modules/mockito-4/src
main/java/com/baeldung/spymethods
test/java/com/baeldung/spymethods
2 files changed +61
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .baeldung .spymethods ;
2
+
3
+ public class CatTantrum {
4
+
5
+ public enum CatAction {
6
+ BITE ,
7
+ MEOW ,
8
+ VOMIT_ON_CARPET ,
9
+ EAT_DOGS_FOOD ,
10
+ KNOCK_THING_OFF_TABLE
11
+ }
12
+ public enum HumanReaction {
13
+ SCREAM ,
14
+ CRY ,
15
+ CLEAN ,
16
+ PET_ON_HEAD ,
17
+ BITE_BACK ,
18
+ }
19
+
20
+ public HumanReaction whatIsHumanReaction (CatAction action ){
21
+ return switch (action ) {
22
+ case MEOW -> HumanReaction .PET_ON_HEAD ;
23
+ case VOMIT_ON_CARPET -> HumanReaction .CLEAN ;
24
+ case EAT_DOGS_FOOD -> HumanReaction .SCREAM ;
25
+ case KNOCK_THING_OFF_TABLE -> HumanReaction .CRY ;
26
+ case BITE -> biteCatBack ();
27
+ };
28
+ }
29
+
30
+ public HumanReaction biteCatBack () {
31
+ // Some logic
32
+ return HumanReaction .BITE_BACK ;
33
+ }
34
+
35
+ }
Original file line number Diff line number Diff line change
1
+ import com .baeldung .spymethods .CatTantrum ;
2
+ import com .baeldung .spymethods .CatTantrum .CatAction ;
3
+ import com .baeldung .spymethods .CatTantrum .HumanReaction ;
4
+ import org .junit .jupiter .api .Test ;
5
+ import org .mockito .Mockito ;
6
+
7
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
8
+ import static org .mockito .Mockito .when ;
9
+
10
+ public class CatTantrumUnitTest {
11
+
12
+ @ Test
13
+ public void givenMockMethodHumanReactions_whenCatActionBite_thenHumanReactionsBiteBack (){
14
+ //Given
15
+ CatTantrum catTantrum = new CatTantrum ();
16
+ CatTantrum catTantrum1 = Mockito .spy (catTantrum );
17
+ Mockito .doReturn (HumanReaction .BITE_BACK ).when (catTantrum1 ).biteCatBack ();
18
+
19
+ //When
20
+ HumanReaction humanReaction1 = catTantrum1 .whatIsHumanReaction (CatAction .BITE );
21
+
22
+ //Then
23
+ assertEquals (humanReaction1 , HumanReaction .BITE_BACK );
24
+ }
25
+
26
+ }
You can’t perform that action at this time.
0 commit comments