From 9bb33102f97badd68ffcba5b17cf4cc7087e80d3 Mon Sep 17 00:00:00 2001 From: Kartik Singla Date: Sat, 18 Aug 2018 11:10:55 +0530 Subject: [PATCH] [BAEL-1858] - How to mock a static method using JMockit (#4925) --- .../baeldung/mocks/jmockit/AppManager.java | 26 +++++++++++ .../mocks/jmockit/AppManagerUnitTest.java | 44 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 testing-modules/mocks/jmockit/src/main/java/com/baeldung/mocks/jmockit/AppManager.java create mode 100644 testing-modules/mocks/jmockit/src/test/java/com/baeldung/mocks/jmockit/AppManagerUnitTest.java diff --git a/testing-modules/mocks/jmockit/src/main/java/com/baeldung/mocks/jmockit/AppManager.java b/testing-modules/mocks/jmockit/src/main/java/com/baeldung/mocks/jmockit/AppManager.java new file mode 100644 index 0000000000..615650ea6d --- /dev/null +++ b/testing-modules/mocks/jmockit/src/main/java/com/baeldung/mocks/jmockit/AppManager.java @@ -0,0 +1,26 @@ +package com.baeldung.mocks.jmockit; + +import java.util.Random; + +public class AppManager { + + public boolean managerResponse(String question) { + return AppManager.isResponsePositive(question); + } + + public static boolean isResponsePositive(String value) { + if (value == null) + return false; + int orgLength = value.length(); + int randomNumber = randomNumber(); + return orgLength == randomNumber ? true : false; + } + + private static int randomNumber() { + return new Random().nextInt(7); + } + + private static Integer stringToInteger(String num) { + return Integer.parseInt(num); + } +} diff --git a/testing-modules/mocks/jmockit/src/test/java/com/baeldung/mocks/jmockit/AppManagerUnitTest.java b/testing-modules/mocks/jmockit/src/test/java/com/baeldung/mocks/jmockit/AppManagerUnitTest.java new file mode 100644 index 0000000000..2c5aa2a3ed --- /dev/null +++ b/testing-modules/mocks/jmockit/src/test/java/com/baeldung/mocks/jmockit/AppManagerUnitTest.java @@ -0,0 +1,44 @@ +package com.baeldung.mocks.jmockit; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import mockit.Deencapsulation; +import mockit.Mock; +import mockit.MockUp; + +public class AppManagerUnitTest { + + private AppManager appManager; + + @BeforeEach + public void setUp() { + appManager = new AppManager(); + } + + @Test + public void givenAppManager_whenStaticMethodCalled_thenValidateExpectedResponse() { + new MockUp() { + @Mock + public boolean isResponsePositive(String value) { + return false; + } + }; + + Assertions.assertFalse(appManager.managerResponse("Why are you coming late?")); + } + + @Test + public void givenAppManager_whenPrivateStaticMethod_thenValidateExpectedResponse() { + final int response = Deencapsulation.invoke(AppManager.class, "stringToInteger", "110"); + Assertions.assertEquals(110, response); + } + + @Test + public void givenAppManager_whenPrivateStaticMethod_thenExpectException() { + Assertions.assertThrows(IllegalArgumentException.class, () -> { + Deencapsulation.invoke(AppManager.class, "stringToInteger", "11r"); + }); + } +}