代码之家  ›  专栏  ›  技术社区  ›  skpraveen

使用非Junit断言方法的TestMethodWithoutAssertion的JQassistant规则

  •  0
  • skpraveen  · 技术社区  · 6 年前

    我们的项目使用 明确肯定 assertj库中的方法以及一些单元测试方法中的方法。因此,当前用于搜索assert方法的cypher规则没有像下面这样识别assert方法,并将其标记为冲突。

    资产(“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 回复  |  直到 6 年前
        1
  •  1
  •   Jens Nerche    6 年前

    是的

    "OR assert.name =~ ".*assert.*"" 
    

    但它有一个缺点,即每个名称中包含assert的方法都会被标记。 注意点后的星星。

    考虑到您想要使用提供的“junit4:TestMethodWithoutAssertion”约束,我建议使用 http://buschmais.github.io/jqassistant/doc/1.3.0/#junit4:AssertMethod 概念:

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

    因此,您现在的组定义如下:

    <group id="default">
        <includeConcept refId="assertj:AssertMethod" />
        <includeConstraint refId="junit4:TestMethodWithoutAssertion" />
    </group>
    

    现在,您提供的示例方法已正确处理。

        2
  •  1
  •   Dirk Mahler    6 年前

    看看Spring PetClinic演示应用程序:它在 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
    ----