junitneo4jcypherjqassistant

JQassistant rule for TestMethodWithoutAssertion with non-Junit assert methods


Our project uses assert methods from assertj library as well in some of the unit test methods. So the current cypher rule to search for assert method doesn't identify assert methods like below and flags them as violations.

assertThat("x").isEqualTo("Y"); in the unit test methods.

how to modify the script to consider any "assert*" invocations from unit test method.

      <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>

Sample Test method :

        @Test
        public void testAssertWithDiffLibrary() {
          String testName = "Hello";
          assertThat(testName).isEqualTo("Hello");
         }

Note : Tried adding a where clause "OR assert.name =~ ".assert."" but doesn't detect these asserts.


Solution

  • It would be

    "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/#junit4:AssertMethod 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.