File tree Expand file tree Collapse file tree
testing-modules/mocks/jmockit/src
main/java/com/baeldung/mocks/jmockit
test/java/com/baeldung/mocks/jmockit Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .mocks .jmockit ;
2+
3+ import java .util .Random ;
4+
5+ public class AppManager {
6+
7+ public boolean managerResponse (String question ) {
8+ return AppManager .isResponsePositive (question );
9+ }
10+
11+ public static boolean isResponsePositive (String value ) {
12+ if (value == null )
13+ return false ;
14+ int orgLength = value .length ();
15+ int randomNumber = randomNumber ();
16+ return orgLength == randomNumber ? true : false ;
17+ }
18+
19+ private static int randomNumber () {
20+ return new Random ().nextInt (7 );
21+ }
22+
23+ private static Integer stringToInteger (String num ) {
24+ return Integer .parseInt (num );
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .mocks .jmockit ;
2+
3+ import org .junit .jupiter .api .Assertions ;
4+ import org .junit .jupiter .api .BeforeEach ;
5+ import org .junit .jupiter .api .Test ;
6+
7+ import mockit .Deencapsulation ;
8+ import mockit .Mock ;
9+ import mockit .MockUp ;
10+
11+ public class AppManagerUnitTest {
12+
13+ private AppManager appManager ;
14+
15+ @ BeforeEach
16+ public void setUp () {
17+ appManager = new AppManager ();
18+ }
19+
20+ @ Test
21+ public void givenAppManager_whenStaticMethodCalled_thenValidateExpectedResponse () {
22+ new MockUp <AppManager >() {
23+ @ Mock
24+ public boolean isResponsePositive (String value ) {
25+ return false ;
26+ }
27+ };
28+
29+ Assertions .assertFalse (appManager .managerResponse ("Why are you coming late?" ));
30+ }
31+
32+ @ Test
33+ public void givenAppManager_whenPrivateStaticMethod_thenValidateExpectedResponse () {
34+ final int response = Deencapsulation .invoke (AppManager .class , "stringToInteger" , "110" );
35+ Assertions .assertEquals (110 , response );
36+ }
37+
38+ @ Test
39+ public void givenAppManager_whenPrivateStaticMethod_thenExpectException () {
40+ Assertions .assertThrows (IllegalArgumentException .class , () -> {
41+ Deencapsulation .invoke (AppManager .class , "stringToInteger" , "11r" );
42+ });
43+ }
44+ }
You can’t perform that action at this time.
0 commit comments