Reputation: 1182
I'm trying to write a test for my project that lets me know, I assume via reflection, if the classes I'm testing might call a specific method.
Note the below example code:
class A implements SomeInterface {
@Override
public process(B bInstance) {
if (Math.rand() < 0.5)
bInstance.thirdPartyCall(param1, param2); //I need this!
}
}
The class(es) override a method process(...) that might call a method from one of the objects passed into process(). I need my test to find out if the class(es) in question has been coded to include any calls to B(), and what the parameters are. If we can only get the invocation as a string, I will hopefully be able to work with that.
We have all the source code for both the classes that override A() and the class that contains B(), but we need this to be dynamic for future maintenance's sake. Since we need this to be dynamic, I can't just create a mock since I won't know if my mock's settings will hit the code that calls the methods from bInstance.
Upvotes: 2
Views: 3056
Reputation: 103777
You wouldn't be able to do this via reflection, as broadly speaking reflection can tell you about the signature of classes, but gives you nothing on their implementations.
In order to do what you're after, you would need to look through the bytecode, and look at the invokevirtual
(or invokestatic
for static methods) codes that appear within each method definition. Using this you could build up a sort of dictionary that determines what calls what. (Presumably this is what IDEs rely on for "find usages" functionality.)
For "standalone" methods you've defined yourself, this might be sufficient. However things will get trickier for methods that override or implement methods defined elsewhere - so if you define your own Runnable
, or Map
subclass for example. It's not easily possible (maybe even provably impossible) to know what the concrete implementation of a method call will be just from inspecting the bytecode, so you couldn't say for sure whether your map's get()
would be called, if the caller simply had a reference to Map
. Equally, if you do have a Runnable
implementation it's unlikely that your class A
will call run
directly; that call is likely to come from somewhere within the standard library (e.g. in an ExecutorService
or from Thread.start()
), so you'd have to build up a very large transitive call map across everything on the classpath, not just your own code.
Plus of course, to bring it back to your initial point - you wouldn't be able to detect reflective calls at all. Method.invoke()
can call just about anything from a static analysis perspective.
I think your intent is good, but unless you can find an existing library to use, it won't be worth the effort to develop this sort of "coverage" metric for your tests.
Upvotes: 4