Updated to make the code simpler to understand for PowerMock usage (#2772)

This commit is contained in:
Sirish Renukumar 2017-10-20 22:38:42 +05:30 committed by maibin
parent 3d7a395b37
commit f5a6fc886e
2 changed files with 7 additions and 6 deletions

View File

@ -4,23 +4,24 @@ class LuckyNumberGenerator {
public int getLuckyNumber(String name) { public int getLuckyNumber(String name) {
record(name); saveIntoDatabase(name);
if (name == null) { if (name == null) {
return getDefaultLuckyNumber(); return getDefaultLuckyNumber();
} }
return getLuckyNumber(name.length() + 1); return getComputedLuckyNumber(name.length());
} }
private void record(String name) { private void saveIntoDatabase(String name) {
// Save the name into the database
} }
private int getDefaultLuckyNumber() { private int getDefaultLuckyNumber() {
return 100; return 100;
} }
private int getLuckyNumber(int length) { private int getComputedLuckyNumber(int length) {
return length < 5 ? 5 : 10000; return length < 5 ? 5 : 10000;
} }

View File

@ -31,7 +31,7 @@ public class LuckyNumberGeneratorTest {
public final void givenPrivateMethodWithArgumentAndReturn_whenUsingPowerMockito_thenCorrect() throws Exception { public final void givenPrivateMethodWithArgumentAndReturn_whenUsingPowerMockito_thenCorrect() throws Exception {
LuckyNumberGenerator mock = spy(new LuckyNumberGenerator()); LuckyNumberGenerator mock = spy(new LuckyNumberGenerator());
doReturn(1).when(mock, "getLuckyNumber", ArgumentMatchers.anyInt()); doReturn(1).when(mock, "getComputedLuckyNumber", ArgumentMatchers.anyInt());
int result = mock.getLuckyNumber("Jack"); int result = mock.getLuckyNumber("Jack");
@ -44,7 +44,7 @@ public class LuckyNumberGeneratorTest {
int result = mock.getLuckyNumber("Tyranosorous"); int result = mock.getLuckyNumber("Tyranosorous");
verifyPrivate(mock).invoke("record", ArgumentMatchers.anyString()); verifyPrivate(mock).invoke("saveIntoDatabase", ArgumentMatchers.anyString());
assertEquals(10000, result); assertEquals(10000, result);
} }