CQL queries for your test code
On the occasion of the release of our second release candidate for XDepend 1.0, I thought I’d write a quick followup on the post related to the analysis of the test code. Among the new features brought by this release is the support for the CQL keyword HasAnnotation which can come in handy in this use case.
In the said post I gave the following CQL query to detect useless tests:
SELECT METHODS WHERE !IsUsing "org.junit" AND !IsConstructor AND ReturnTypeIs "void" AND !NameIs "setUp()" AND IsPublic
This query seems to do the job, however it uses several conditions to make sure that the method is a test and it isn’t impossible to get false positives. If your project is relying on annotations to mark tests instead of subclassing and naming conventions, there is now a better way to query for tests. By taking advantage of the HasAnnotation, you can rewrite the query this way:
SELECT METHODS WHERE !IsUsing "org.junit.Assert" AND HasAnnotation "org.junit.Test"
Be sure to check out the release notes of XDepend 1.0 RC2 for the other remarkable changes.