Правило JQassistant для TestMethodWithoutAssertion с не-Junit методами assert
Наш проект использует методы assert из библиотеки assertj также в некоторых методах модульного тестирования. Таким образом, текущее правило шифра для поиска метода assert не идентифицирует методы assert, как показано ниже, и помечает их как нарушения.
assertThat ("X") isEqualTo("Y"). в методах модульного тестирования.
как изменить скрипт, чтобы он учитывал любые вызовы "assert*" из метода модульного теста.
<cypher><![CDATA[
MATCH
(c:Type:Class)-[:DECLARES]->(m:Method:JunitTestMethod),
(m)-[:INVOKES*..10]->(assert:Method)
WHERE
assert.signature =~ "void assert.*"
OR assert.signature =~ "void fail.*"
OR assert.signature =~ "void verify.*"
SET
m:JunitTestWithAssert
RETURN
c.fqn AS TestClass, m.name AS TestMethodWithAssert
]]></cypher>
Метод испытания образца:
@Test
public void testAssertWithDiffLibrary() {
String testName = "Hello";
assertThat(testName).isEqualTo("Hello");
}
Примечание. Попытка добавления предложения where "ИЛИ assert.name =~ ".утверждать."" но не обнаруживает эти утверждения.
2 ответа
Это было бы
"OR assert.name =~ ".*assert.*""
But it has the drawback that every method containing assert in name is marked. Note the stars after the dots.
Given that you want to use the provided "junit4:TestMethodWithoutAssertion" constraint, I would suggest the AssertJ version of the http://buschmais.github.io/jqassistant/doc/1.3.0/ concept:
<concept id="assertj:AssertMethod">
<description>Labels all assertion methods declared by org.assertj.core.api.Assertions with "Assertj" and "Assert".</description>
<cypher><![CDATA[
MATCH
(assertType:Type)-[:DECLARES]->(assertMethod)
WHERE
assertType.fqn = 'org.assertj.core.api.Assertions'
and assertMethod.signature =~ '.*assertThat.*'
SET
assertMethod:Assertj:Assert
RETURN
assertMethod
]]></cypher>
</concept>
So your group definition now is as follows:
<group id="default">
<includeConcept refId="assertj:AssertMethod" />
<includeConstraint refId="junit4:TestMethodWithoutAssertion" />
</group>
Now your provided sample method is handled correctly.
Взгляните на демонстрационное приложение Spring PetClinic: оно предоставляет концепцию assertj:AssertMethod в assertj.adoc:
[[assertj:AssertMethod]]
[source,cypher,role=concept]
.Mark all assertThat methods of 'org.assertj.core.api.Assertions' with "AssertJ" and "Assert".
----
MATCH
(assertType:Type)-[:DECLARES]->(assertMethod)
WHERE
assertType.fqn = 'org.assertj.core.api.Assertions'
and assertMethod.signature =~ '.* assertThat.*'
SET
assertMethod:AssertJ:Assert
RETURN
assertMethod
----
на который затем ссылается ограничение в test.adoc:
[[test:TestMethodWithoutAssertion]]
[source,cypher,role=constraint,requiresConcepts="junit4:TestMethod,assertj:AssertMethod,spring-test-web:Assert"]
.All test methods must perform at least one assertion (within a call hierarchy of max. 3 steps).
----
MATCH
(testType:Test:Type)-[:DECLARES]->(testMethod:Test:Method)
WHERE
NOT (testMethod)-[:INVOKES*..3]->(:Method:Assert)
RETURN
testType AS DeclaringType,
testMethod AS Method
----