Reputation: 97
I'm using PowerMock/EasyMock to test a static method in which one of the parameters is a StringBuffer that is appended to by a method in that mocked class.
This is a simplified class to demonstrate.
import java.util.Date;
public class ContentChanger
{
public static int change(StringBuffer sb)
{
sb.append( new Date() );
return 0;
}
}
And here is the unit test...
import org.easymock.EasyMock;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest(ContentChanger.class)
public class ContentChangerTest
{
@Test
public void test001()
{
// Declare an empty StringBuffer
StringBuffer var = new StringBuffer();
// Enable static mocking for the ContentChanger class
PowerMock.mockStatic( ContentChanger.class );
// Catch the call and send to test method
EasyMock.expect(ContentChanger.change( var )).andDelegateTo( test(var) );
// Replay all mock classes/methods
PowerMock.replayAll();
// Call the method to be mocked
System.out.println( ContentChanger.change( var ) + " = " + var );
}
private int test( StringBuffer sb )
{
sb.append( "Mocked" );
return 1;
}
}
What I expect to happen is that the test method is called and the StringBuffer to output..
1 = MOCKED
But what is happening is the StringBuffer var is updated before the mocked method is called.
i.e. I get the following...
java.lang.AssertionError:
Unexpected method call ContentChanger.change(Mocked):
ContentChanger.change(Mocked): expected: 1, actual: 2
Is there a way to invoke another class/method, to change the contents of the parameter when called instead of pre-replay.
Upvotes: 0
Views: 1835
Reputation: 5731
The problem is that you are calling test(var)
. andDelegateTo
expects an Object that will be of the same class/interface as the mock. See http://easymock.org/user-guide.html#verification-creating.
Since you are on a static method, this is not really possible. So the best is to use IAnswer instead. Here is the working code:
EasyMock.expect(ContentChanger.change( var )).andAnswer(new IAnswer<Integer>() {
@Override
public Integer answer() throws Throwable {
StringBuffer sb = (StringBuffer) EasyMock.getCurrentArguments()[0];
sb.append( "Mocked" );
return 1;
}
})
Upvotes: 1