Skip to content

Commit 0d3bc92

Browse files
SONARJAVA-6208 S2699: Add approve to assertion method name pattern (#5570)
Co-authored-by: sonar-review-alpha[bot] <266116024+sonar-review-alpha[bot]@users.noreply.github.com>
1 parent 63bef71 commit 0d3bc92

6 files changed

Lines changed: 42 additions & 2 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ruleKey": "S2699",
33
"hasTruePositives": true,
4-
"falseNegatives": 157,
4+
"falseNegatives": 158,
55
"falsePositives": 1
66
}

java-checks-test-sources/default/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,12 @@
10161016
<artifactId>junit-platform-suite</artifactId>
10171017
<scope>provided</scope>
10181018
</dependency>
1019+
<dependency>
1020+
<groupId>org.approvej</groupId>
1021+
<artifactId>core</artifactId>
1022+
<version>1.6.0</version>
1023+
<scope>test</scope>
1024+
</dependency>
10191025
</dependencies>
10201026

10211027
<profiles>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package checks.tests.AssertionsInTestsCheck;
2+
3+
import java.time.LocalDate;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.approvej.ApprovalBuilder.approve;
7+
import static org.approvej.print.MultiLineStringPrintFormat.multiLineString;
8+
9+
class ApproveJTest {
10+
11+
@Test
12+
void contains_no_assertions() { // Noncompliant
13+
}
14+
15+
public String hello() {return "hello";}
16+
17+
@Test
18+
void approve_string() {
19+
String result = hello();
20+
approve(result)
21+
.byFile();
22+
}
23+
24+
public record Person(String name, LocalDate birthDate) {}
25+
26+
@Test
27+
void approve_person() {
28+
Person jane = new Person("Jane Doe", LocalDate.of(1990, 1, 1));
29+
approve(jane).named("jane").printedAs(multiLineString()).byFile();
30+
}
31+
32+
}

java-checks/src/main/java/org/sonar/java/checks/helpers/UnitTestUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public final class UnitTestUtils {
4141

4242
private static final String ORG_JUNIT_TEST = "org.junit.Test";
4343
public static final Pattern ASSERTION_METHODS_PATTERN = Pattern.compile(
44-
"(assert|verify|fail|should|check|expect|validate|andExpect).*" +
44+
"(assert|verify|fail|should|check|expect|validate|andExpect|approve).*" +
4545
// Eclipse Vert.x with JUnit 5 (VertxTestContext)
4646
"|laxCheckpoint|succeedingThenComplete");
4747
private static final Pattern TEST_METHODS_PATTERN = Pattern.compile("test.*|.*Test");

java-checks/src/test/java/org/sonar/java/checks/tests/AssertionsInTestsCheckTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ void setup() {
6464
"JMockit",
6565
"Awaitility",
6666
"AssertJ",
67+
"ApproveJ",
6768
"Custom"
6869
})
6970
void test(String framework) {

sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S2699.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ <h2>Why is this an issue?</h2>
33
code under test.</p>
44
<p>This rule raises an exception when no assertions from any of the following known frameworks are found in a test:</p>
55
<ul>
6+
<li>ApproveJ</li>
67
<li>AssertJ</li>
78
<li>Awaitility</li>
89
<li>EasyMock</li>

0 commit comments

Comments
 (0)