codeql
codeql copied to clipboard
[Java] Extract all unit test methods and the methods they call
I want to extract all unit test methods (Junit 4 or 5) and the corresponding methods that they test. I want to learn how to write a query for this.
Based on my current knowledge, I can do a from Call call and then add some heuristic-based checks on call.getCallee() and call.getCaller().
For example:
/**
* @name Call graph
* @description Call graph.
* @kind problem
* @id java/call-graph
* @tags testability
*/
import java
// Track calls from test methods to other methods within the codebase.
from Call call
where call.getCaller().getName().matches("%test%")
select call.getCallee(), "calls", call.getCaller()
I get the following error: Error was: Expected result pattern(s) are not present for problem query: Expected alternating entity/string and string columns. [INVALID_RESULT_PATTERNS]
What am I doing wrong here? Also, what would be a better way to write this query?
A query with @kind problem should select a location, a message followed by zero or more pairs of element and link text.
In this case you could write select call.getCallee(), "is called by $@", call.getCaller(), call.getCaller().getName()