Updated to make the code simpler to understand for PowerMock usage (#2772)
This commit is contained in:
parent
3d7a395b37
commit
f5a6fc886e
|
@ -4,23 +4,24 @@ class LuckyNumberGenerator {
|
|||
|
||||
public int getLuckyNumber(String name) {
|
||||
|
||||
record(name);
|
||||
saveIntoDatabase(name);
|
||||
|
||||
if (name == null) {
|
||||
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() {
|
||||
return 100;
|
||||
}
|
||||
|
||||
private int getLuckyNumber(int length) {
|
||||
private int getComputedLuckyNumber(int length) {
|
||||
return length < 5 ? 5 : 10000;
|
||||
}
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ public class LuckyNumberGeneratorTest {
|
|||
public final void givenPrivateMethodWithArgumentAndReturn_whenUsingPowerMockito_thenCorrect() throws Exception {
|
||||
LuckyNumberGenerator mock = spy(new LuckyNumberGenerator());
|
||||
|
||||
doReturn(1).when(mock, "getLuckyNumber", ArgumentMatchers.anyInt());
|
||||
doReturn(1).when(mock, "getComputedLuckyNumber", ArgumentMatchers.anyInt());
|
||||
|
||||
int result = mock.getLuckyNumber("Jack");
|
||||
|
||||
|
@ -44,7 +44,7 @@ public class LuckyNumberGeneratorTest {
|
|||
|
||||
int result = mock.getLuckyNumber("Tyranosorous");
|
||||
|
||||
verifyPrivate(mock).invoke("record", ArgumentMatchers.anyString());
|
||||
verifyPrivate(mock).invoke("saveIntoDatabase", ArgumentMatchers.anyString());
|
||||
assertEquals(10000, result);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue