具有 lambda 表达式和消费者断言的 TestMethods 的 JQassistant 规则

JQassistant rule for TestMethods with lambda expressions and consumers asserts

我们的项目很少有单元测试,其中断言作为 lambda 或消费者传递给测试 class。示例如下。 如何编写密码规则约束 这样可以识别断言并且不会将方法标记为没有断言。目前正在使用 junit4:TestMethodWithoutAssertion

测试方法:

     @Test 
     public void testSuccessfulIdempotency(){
     transportConsumerFlow.accept(Mockito.mock(TransRequest.class), 
     (t)->{
           assertEquals(t, expectedResponseMessage);
      });
     }

在上面的示例中,断言实际上存在并且有效。但是无法检测到概念 junit4:AssertMethod 可能是因为它作为消费者存在,而不是在 Test 方法中直接调用。

Lambda 表达式目前未被 jQAssistant 明确支持,但您可以使用以下概念将它们识别为合成静态方法(由字节码生成):

MATCH
  (type:Type)-[:DECLARES]->(lambda:Method)
WHERE
  exists(lambda.synthetic)
  and exists(lambda.static)
  and lambda.name starts with("lambda$")
SET
  lambda:Lambda
WITH
  type, lambda
MATCH
  (type)-[:DECLARES]->(method:Method)
WHERE
  method <> lambda
  and method.firstLineNumber <= lambda.firstLineNumber
  and method.lastLineNumber >= lambda.lastLineNumber
MERGE
  (method)-[:DECLARES_LAMBDA]->(lambda)
RETURN
  method, collect(lambda)

您不会看到从测试方法到 lambda 方法的任何 INVOKES 关系,因此需要使用具有以下密码查询的自定义约束(基于 junit4:TestMethodWithoutAssertion):

MATCH
  (testType:Type)-[:DECLARES]->(testMethod:Test:Method)
WHERE
  NOT (testMethod)-[:INVOKES|DECLARES_LAMBDA*..3]->(:Method:Assert)
RETURN
  testType AS DeclaringType,
  testMethod AS Method