问题描述
我们的项目几乎没有单元测试,其中断言作为lambda或consumer传递给测试类。示例如下。 如何编写密码规则约束这样的断言被识别,并且该方法没有被标记为没有断言。目前正在使用junit4:TestMethodWithoutAssertion
Our project has few Unit Tests in which the asserts are passed as a lambda or consumer to the test class. Example as below. How to write a cypher rule constraint such the asserts are identified and the method is not flagged as without assert. Currently using junit4:TestMethodWithoutAssertion
测试方法:
@Test
public void testSuccessfulIdempotency(){
transportConsumerFlow.accept(Mockito.mock(TransRequest.class),
(t)->{
assertEquals(t, expectedResponseMessage);
});
}
在上面的示例中,断言实际存在且有效。但是,无法检测到概念junit4:AssertMethod可能是因为它作为消费者而不是在Test方法中直接调用。
In the sample above, the assert is actually present and valid. But the concept junit4:AssertMethod could not detected may be because it is present as a consumer instead of a direct invocation in Test method.
推荐答案
Lambda表达式当前并未由jQAssistant明确支持,但您可以将它们识别为合成静态方法(由字节码生成)使用以下概念:
Lambda expressions are currently not explicitly support by jQAssistant but you may identify them as synthetic static methods (as generated by the bytecode) using the following concept:
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关系,因此需要使用带有以下cypher查询的自定义约束(基于junit4:TestMethodWithoutAssertion):
You'll not see any INVOKES relations from the test methods to the lambda methods, so a custom constraint with the following cypher query needs to be used (based on 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
这篇关于具有lambda表达式和使用者断言的TestMethods的JQassistant规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!