There is an example in EasyMock for mocking native methods. Hope it helps.
The source code could be found here in github repository.
package samples.nativemocking;
/**
* The purpose of this class is to demonstrate that it's possible to mock native
* methods using plain EasyMock class extensions.
*/
public class NativeService {
public native String invokeNative(String nativeParameter);
}
package samples.nativemocking;
/**
* The purpose of this class is to invoke a native method in a collaborator.
*/
public class NativeMockingSample {
private final NativeService nativeService;
public NativeMockingSample(NativeService nativeService) {
this.nativeService = nativeService;
}
public String invokeNativeMethod(String param) {
return nativeService.invokeNative(param);
}
}
package samples.junit4.nativemocking;
import org.junit.Test;
import samples.nativemocking.NativeMockingSample;
import samples.nativemocking.NativeService;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertEquals;
/**
* This test demonstrates that it's possible to mock native methods using plain
* EasyMock class extensions.
*/
public class NativeMockingSampleTest {
@Test
public void testMockNative() throws Exception {
NativeService nativeServiceMock = createMock(NativeService.class);
NativeMockingSample tested = new NativeMockingSample(nativeServiceMock);
final String expectedParameter = "question";
final String expectedReturnValue = "answer";
expect(nativeServiceMock.invokeNative(expectedParameter)).andReturn(expectedReturnValue);
replay(nativeServiceMock);
assertEquals(expectedReturnValue, tested.invokeNativeMethod(expectedParameter));
verify(nativeServiceMock);
}
}