Add tests

This commit is contained in:
Clément Fournier committed 2024-03-21 19:13:15 +01:00
1 parent 29affeedc4
commit 8c4eb75110
4 files changed
+45 -10

No files matched your search

@@ -34,11 +34,11 @@ public class JUnitTestsShouldIncludeAssertRule extends AbstractJavaRulechainRule
ASTBlock body = method.getBody();
Set<String> extraAsserts = getProperty(EXTRA_ASSERT_METHOD_NAMES);
if (body != null
&& TestFrameworksUtil.isTestMethod(method)
&& !TestFrameworksUtil.isExpectAnnotated(method)
&& body.descendants(ASTMethodCall.class)
.none(call -> TestFrameworksUtil.isProbableAssertCall(call)
|| extraAsserts.contains(call.getMethodName()))) {
&& TestFrameworksUtil.isTestMethod(method)
&& !TestFrameworksUtil.isExpectAnnotated(method)
&& body.descendants(ASTMethodCall.class)
.none(call -> TestFrameworksUtil.isProbableAssertCall(call)
|| extraAsserts.contains(call.getMethodName()))) {
asCtx(data).addViolation(method);
}
return data;
@@ -174,11 +174,11 @@ public final class TestFrameworksUtil {
public static boolean isProbableAssertCall(ASTMethodCall call) {
String name = call.getMethodName();
return name.startsWith("assert") && !isSoftAssert(call)
|| name.startsWith("check")
|| name.startsWith("verify")
|| "fail".equals(name)
|| "failWith".equals(name)
|| isExpectExceptionCall(call);
|| name.startsWith("check")
|| name.startsWith("verify")
|| "fail".equals(name)
|| "failWith".equals(name)
|| isExpectExceptionCall(call);
}
private static boolean isSoftAssert(ASTMethodCall call) {
@@ -272,4 +272,23 @@ public class MyTestCase {
}
]]></code>
</test-code>
<test-code>
<description>Extra properties</description>
<rule-property name="extraAssertMethodNames">customAssertion</rule-property>
<expected-problems>1</expected-problems>
<code><![CDATA[
import org.junit.jupiter.api.Test;
public class A {
@Test
void test() {
customAssertion("aueoau");
customAssertion("aueoau");
customAssertion("aueoau");
}
}
]]></code>
</test-code>
</test-data>
@@ -773,4 +773,20 @@ public class CustomAssertTests {
}
]]></code>
</test-code>
<test-code>
<description>Extra properties</description>
<rule-property name="extraAssertMethodNames">customAssertion</rule-property>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.junit.jupiter.api.Test;
public class A {
@Test
void test() {
customAssertion("aueoau");
}
}
]]></code>
</test-code>
</test-data>