I have done a thorough search through all of the PowerMock-EasyMock documentation, forums, etc. and have not found a definitive answer to my above-mentioned issue.
My current software: jdk1.7.0_55
powermock-easymock 1.6.0_full
EasyMock 3.2
I have looked at the section in the documentation that describes mocking system classes, but this only shows how to mock system classes with static methods.
There is contradictory information in the forums and Google posts about mocking the toString() method; some saying it cannot be done for any object because javassist bypasses those calls.
Does anyone have the definitive answer on mocking toString(), specifically in class StringBuilder?
I have tried the following:
String myExportFileName = "myExportFile";
MemberModifier.stub(Whitebox.getMethod(StringBuilder.class, "toString")).toReturn(myExportFileName);
This returns the actual file name from the implementation instead of myExportFileName.
The documentation states that we cannot call methods in system classes in the same manner as we call them in user-defined classes, for example, the following would not be expected to work, and it doesn't:
StringBuilder sb = createMock(StringBuilder.class);
expect(sb.toString()).andStubReturn(myExportFileName);
Can anyone shed some light on this issue. There does not appear to be a definitive answer anywhere.
Thanks in advance for anyone's help.
- R Jacobsen
09/04/2015