From f92aa19c37532132c5951fc62bf03c760ee2bbee Mon Sep 17 00:00:00 2001 From: Sirish Renukumar Date: Fri, 20 Oct 2017 10:41:06 +0530 Subject: [PATCH] Mocking and verification of private methods using PowerMock (#2756) --- .../introduction/LuckyNumberGenerator.java | 27 ++++++++++ .../LuckyNumberGeneratorTest.java | 51 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 mockito/src/test/java/com/baeldung/powermockito/introduction/LuckyNumberGenerator.java create mode 100644 mockito/src/test/java/com/baeldung/powermockito/introduction/LuckyNumberGeneratorTest.java diff --git a/mockito/src/test/java/com/baeldung/powermockito/introduction/LuckyNumberGenerator.java b/mockito/src/test/java/com/baeldung/powermockito/introduction/LuckyNumberGenerator.java new file mode 100644 index 0000000000..7cb30b9fef --- /dev/null +++ b/mockito/src/test/java/com/baeldung/powermockito/introduction/LuckyNumberGenerator.java @@ -0,0 +1,27 @@ +package com.baeldung.powermockito.introduction; + +class LuckyNumberGenerator { + + public int getLuckyNumber(String name) { + + record(name); + + if (name == null) { + return getDefaultLuckyNumber(); + } + + return getLuckyNumber(name.length() + 1); + } + + private void record(String name) { + } + + private int getDefaultLuckyNumber() { + return 100; + } + + private int getLuckyNumber(int length) { + return length < 5 ? 5 : 10000; + } + +} diff --git a/mockito/src/test/java/com/baeldung/powermockito/introduction/LuckyNumberGeneratorTest.java b/mockito/src/test/java/com/baeldung/powermockito/introduction/LuckyNumberGeneratorTest.java new file mode 100644 index 0000000000..a66df02a29 --- /dev/null +++ b/mockito/src/test/java/com/baeldung/powermockito/introduction/LuckyNumberGeneratorTest.java @@ -0,0 +1,51 @@ +package com.baeldung.powermockito.introduction; + +import static org.junit.Assert.assertEquals; +import static org.powermock.api.mockito.PowerMockito.doReturn; +import static org.powermock.api.mockito.PowerMockito.spy; +import static org.powermock.api.mockito.PowerMockito.verifyPrivate; +import static org.powermock.api.mockito.PowerMockito.when; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentMatchers; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +@PrepareForTest(fullyQualifiedNames = "com.baeldung.powermockito.introduction.LuckyNumberGenerator") +public class LuckyNumberGeneratorTest { + + @Test + public final void givenPrivateMethodWithReturn_whenUsingPowerMockito_thenCorrect() throws Exception { + LuckyNumberGenerator mock = spy(new LuckyNumberGenerator()); + + when(mock, "getDefaultLuckyNumber").thenReturn(300); + + int result = mock.getLuckyNumber(null); + + assertEquals(300, result); + } + + @Test + public final void givenPrivateMethodWithArgumentAndReturn_whenUsingPowerMockito_thenCorrect() throws Exception { + LuckyNumberGenerator mock = spy(new LuckyNumberGenerator()); + + doReturn(1).when(mock, "getLuckyNumber", ArgumentMatchers.anyInt()); + + int result = mock.getLuckyNumber("Jack"); + + assertEquals(1, result); + } + + @Test + public final void givenPrivateMethodWithNoArgumentAndReturn_whenUsingPowerMockito_thenCorrect() throws Exception { + LuckyNumberGenerator mock = spy(new LuckyNumberGenerator()); + + int result = mock.getLuckyNumber("Tyranosorous"); + + verifyPrivate(mock).invoke("record", ArgumentMatchers.anyString()); + assertEquals(10000, result); + } + +}