如何修改JQassistant Cypher规则识别AssertJ的assert*断言方法
Got it, let's get that JQassistant rule sorted so it stops flagging test methods using AssertJ's assertThat() as "no assertions present". Here's the adjusted Cypher query and breakdown of the fix:
Modified Cypher Rule
MATCH (c:Type:Class)-[:DECLARES]->(m:Method:JunitTestMethod), (m)-[:INVOKES*..10]->(assert:Method) WHERE assert.name STARTS WITH 'assert' WITH m, count(assert) as assertionCount WHERE assertionCount = 0 RETURN m.fullName as TestMethodWithoutAssertions
Key Changes Explained
Added a flexible assertion method filter: The
WHERE assert.name STARTS WITH 'assert'clause ensures we capture any method whose name starts with "assert". This covers:- Traditional JUnit assertions like
assertEquals(),assertTrue() - AssertJ's
assertThat()(since it follows the "assert*" naming pattern) - Any custom assertion methods you might have that use the standard "assert" prefix
- Traditional JUnit assertions like
Eliminates hardcoded method limitations: If your original rule only matched specific JUnit assertion method names, this change makes it flexible enough to support all assertion libraries that adhere to the common "assert" naming convention.
Optional: Narrow to Known Assertion Classes (Edge Case Fix)
If you want to avoid accidental matches with non-assertion methods that start with "assert" (a rare scenario), you can add an extra condition to limit matches to trusted assertion libraries:
MATCH (c:Type:Class)-[:DECLARES]->(m:Method:JunitTestMethod), (m)-[:INVOKES*..10]->(assert:Method) WHERE assert.name STARTS WITH 'assert' AND assert.declaringType.name IN [ 'org.junit.Assert', 'org.assertj.core.api.Assertions', 'org.assertj.core.api.AbstractAssert' ] WITH m, count(assert) as assertionCount WHERE assertionCount = 0 RETURN m.fullName as TestMethodWithoutAssertions
This ensures we only count assertions from established libraries, not random utility methods that happen to start with "assert".
内容的提问来源于stack exchange,提问作者skpraveen

