Skip to content

Commit

Permalink
SONARJAVA-5256 Generalize the pattern for AssertJ assertions
Browse files Browse the repository at this point in the history
The previous conditions were too restrictive and there are many assertion methods in AssertJ. They generally follow the patterns that are described here.
Note that this introduces a false negative in autoscan tests.

Co-authored-by: Alban Auzeill <alban.auzeill@sonarsource.com>
  • Loading branch information
romainbrenguier and alban-auzeill committed Feb 10, 2025
1 parent 5218f3a commit 90e6217
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ruleKey": "S2699",
"hasTruePositives": true,
"falseNegatives": 151,
"falseNegatives": 152,
"falsePositives": 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Set;
import java.util.regex.Pattern;
import javax.annotation.Nullable;

import org.sonar.java.annotations.VisibleForTesting;
import org.sonar.java.model.ExpressionUtils;
import org.sonar.plugins.java.api.semantic.MethodMatchers;
import org.sonar.plugins.java.api.semantic.Symbol;
Expand All @@ -42,6 +44,10 @@ public final class UnitTestUtils {
"|laxCheckpoint|succeedingThenComplete");
private static final Pattern TEST_METHODS_PATTERN = Pattern.compile("test.*|.*Test");

@VisibleForTesting
static final Pattern ASSERTJ_ASSERTION_METHODS_PATTERN = Pattern.compile(
"(allMatch|assert|contains|doesNot|has|is|returns|satisfies)([A-Z].*)?");

public static final MethodMatchers ASSERTION_INVOCATION_MATCHERS = MethodMatchers.or(
// fest 1.x / 2.X
MethodMatchers.create().ofSubTypes("org.fest.assertions.GenericAssert", "org.fest.assertions.api.AbstractAssert").anyName().withAnyParameters().build(),
Expand All @@ -62,8 +68,8 @@ public final class UnitTestUtils {
.withAnyParameters()
.build(),
// assertJ
MethodMatchers.create().ofSubTypes("org.assertj.core.api.AbstractAssert").anyName().withAnyParameters().build(),
MethodMatchers.create().ofSubTypes("org.assertj.core.api.ThrowableTypeAssert").anyName().withAnyParameters().build(),
MethodMatchers.create().ofType(type -> type.fullyQualifiedName().matches("org\\.assertj\\.core\\.api\\.[a-zA-Z]+Assert"))
.name(ASSERTJ_ASSERTION_METHODS_PATTERN.asMatchPredicate()).withAnyParameters().build(),
// spring
MethodMatchers.create().ofTypes("org.springframework.test.web.servlet.ResultActions").names("andExpect", "andExpectAll").withAnyParameters().build(),
// JMockit
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* SonarQube Java
* Copyright (C) 2012-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonar.java.checks.helpers;

import org.junit.jupiter.api.Test;

import java.util.function.Predicate;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;


class UnitTestUtilsTest {

@Test
void testAssertJAssertionMethodPattern() {
Predicate<String> predicate = UnitTestUtils.ASSERTJ_ASSERTION_METHODS_PATTERN.asMatchPredicate();
assertTrue(predicate.test("returns"));
assertTrue(predicate.test("contains"));
assertFalse(predicate.test("doesNot"));
assertTrue(predicate.test("containsAString"));
assertTrue(predicate.test("doesNotThrow"));
assertFalse(predicate.test("allMatchFoo"));
assertFalse(predicate.test("hasten"));
}
}

0 comments on commit 90e6217

Please sign in to comment.