Reputation: 331
I want use a Powermock to test a private method (one) but this private method relies on another private method (two).
So I have to mock the other private method. But while I am debugging it, it turns out that the private method one is not calling the mocked private method (two) and if I run instead of debugging it throws out exception:
1matchers expected, 2 recorded.
private int getCurrentLocaleID(WebIServerSession currentSession, String preferenceName) {
String prefLocaleID = getUserPreference(currentSession, preferenceName);
int lcid;
if (HTTPHelper.isDefaultLocale(prefLocaleID)) {
prefLocaleID = _appContext.getBrowserHeaderLocaleId();
}
try {
lcid = Integer.parseInt(prefLocaleID);
} catch (NumberFormatException nfe) {
lcid = DEFAULT_LCID; // default behavior from old session manager
}
return lcid;
}
@Test
public void getCurrentLocaleID() throws Exception {
PowerMockito.mockStatic(HTTPHelper.class);
WebAppSessionManagerImpl webAppSessionMangerImpl2 = PowerMockito.spy(new WebAppSessionManagerImpl(appConext));
given(HTTPHelper.isDefaultLocale("1")).willReturn(true);
given(HTTPHelper.isDefaultLocale("0")).willReturn(false);
given(appConext.getBrowserHeaderLocaleId()).willReturn("1");
PowerMockito.doReturn("1").when(webAppSessionMangerImpl2, "getUserPreference", anyObject(), anyString());
int result = Whitebox.invokeMethod(webAppSessionMangerImpl2, "getCurrentLocaleID", webIserverSession, "test");
assertEquals(result, 1);
}
Upvotes: 0
Views: 312
Reputation: 26522
Dont test private methods. If you have to, that means that your class is doing too much than it supposed to and it does not comply with the Single Responsibility Principle.
This is a chance for some refactoring and isolation of logic in specialized class like something follwing:
public class SpecializedClass{
private Context context;
public SpecializedClass(Context context){
this.context = context;
}
public int getCurrentLocaleID(WebIServerSession currentSession, String preferenceName) {
String prefLocaleID = getUserPreference(currentSession, preferenceName);
int lcid;
if (HTTPHelper.isDefaultLocale(prefLocaleID)) {
prefLocaleID = _appContext.getBrowserHeaderLocaleId();
}
try {
lcid = Integer.parseInt(prefLocaleID);
} catch (NumberFormatException nfe) {
lcid = DEFAULT_LCID; // default behavior from old session manager
}
return lcid;
}
String getUserPreference(Session session, String preferenceName){..}
}
Now haiving the method public and the getUserPreference
marked as package level, the test would look something like:
@Test
public void getCurrentLocaleID() throws Exception {
PowerMockito.mockStatic(HTTPHelper.class);
SpecializedClass specializedClassSpy = Mockito.spy(new SpecializedClass(appConext));
given(HTTPHelper.isDefaultLocale("1")).willReturn(true);
given(HTTPHelper.isDefaultLocale("0")).willReturn(false);
given(appConext.getBrowserHeaderLocaleId()).willReturn("1");
Mockito.doReturn("1").when(specializedClassSpy)
.getUserPreference(webIserverSession, "test");
int result = specializedClassSpy.getCurrentLocaleID(webIserverSession, "test");
assertEquals(result, 1);
}
Upvotes: 1