Move the call to ArrayDescriptor.createDescriptor( ... )
off into a one-line method of its own class (which can be an inner class of the class where you're using it). Add a field to the class where you're using this call, that has an instance of the new class. Use either a setter or a special constructor to set this field to a value that's different from the usual one. Then mock the inner class, and have it return a mock of ArrayDescriptor
. So you might get something like this.
public class MyClass{
class ArrayDescriptorFactory{
makeArrayDescriptor(String name, Connection connection){
return ArrayDescriptor.createDescriptor(name, connection);
}
}
private ArrayDescriptorFactory factory;
public MyClass(){
this( new ArrayDescriptorFactory());
}
MyClass(ArrayDescriptorFactory factory){
this.factory = factory;
}
public void doSomething(){
... do stuff ...
factory.makeArrayDescriptor( "something", someConnection );
... do more stuff ...
}
}
and in the test
@Mock private MyClass.ArrayDescriptorFactory mockFactory;
@Mock private ArrayDescriptor mockArrayDescriptor;
...
initMocks( this );
when( mockFactory.makeArrayDescriptor( anyString(), any( Connection.class )))
.thenReturn( mockArrayDescriptor );
MyClass toTest = new MyClass( mockFactory );