Mocking an object that uses jni using EasyMock
Posted
by
Visage
on Stack Overflow
See other posts from Stack Overflow
or by Visage
Published on 2010-12-23T15:16:34Z
Indexed on
2010/12/24
10:54 UTC
Read the original article
Hit count: 364
So my class under test has code that looks braodly like this
public void doSomething(int param)
{
Report report = new Report()
...do some calculations
report.someMethod(someData)
}
my intention was to extract the construction of report into a protected method and override it to use a mock object that I could then test to ensure that someMethod had been called with the right data.
So far so good. But Report isnt under my control, and to mkae things worse it uses JNI to load a library at runtime.
If I do Report report = EasyMock.createMock(Report.class)
then EasyMock attempts to use reflection to find out the class members, but this causes an attempt to load the JNI library, which fails (the JNI libraries are only available on UNIX).
Im considering two things: a) Introduce a ReportWrapper interface with two implementations, one of which will delegate calls to an real Report (so basically a Proxy), and a second which will basically use a mock object. or b) instead of calling someMethod, call a protected method which will in turn call someMethod that I can override in a testing subclass.
Either way it seems nasty. Any better ways?
© Stack Overflow or respective owner