Skip to content

Commit b2d7a8b

Browse files
SONARJAVA-5547 Fix S2699 for springframework.util.Assert (#5135)
1 parent 31a7a38 commit b2d7a8b

4 files changed

Lines changed: 41 additions & 1 deletion

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": 154,
4+
"falseNegatives": 155,
55
"falsePositives": 1
66
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package checks.tests.AssertionsInTestsCheck;
2+
3+
import java.net.http.HttpResponse;
4+
import org.junit.Test;
5+
import org.springframework.util.Assert;
6+
7+
public class SpringUtilAssertTestSample {
8+
9+
HttpResponse<String> response;
10+
11+
@Test
12+
public void test_with_no_assertions() { // Noncompliant
13+
// no assertions
14+
}
15+
16+
@org.junit.Test
17+
public void test_spring_util_assert() {
18+
org.springframework.util.Assert.isTrue(response.statusCode() == 200, "Expected 200 ");
19+
}
20+
21+
@Test
22+
public void test_spring_util_assert2() {
23+
Assert.isNull(response, "Expected null but was not");
24+
}
25+
26+
@Test
27+
public void test_spring_util_assert3() {
28+
Assert.hasLength(response.body(), "body should not be empty");
29+
}
30+
31+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public final class UnitTestUtils {
7878
.name(ASSERTJ_ASSERTION_METHODS_PREDICATE).withAnyParameters().build(),
7979
// spring
8080
MethodMatchers.create().ofTypes("org.springframework.test.web.servlet.ResultActions").names("andExpect", "andExpectAll").withAnyParameters().build(),
81+
MethodMatchers.create().ofTypes("org.springframework.util.Assert").anyName().withAnyParameters().build(),
8182
// JMockit
8283
MethodMatchers.create().ofTypes("mockit.Verifications").constructor().withAnyParameters().build(),
8384
// Eclipse Vert.x with JUnit 4

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,12 @@ void testSpringBootAssertableApplicationContext(){
126126
.verifyIssues();
127127
}
128128

129+
@Test
130+
void testSpringUtilAssert(){
131+
CheckVerifier.newVerifier()
132+
.onFile(testCodeSourcesPath("checks/tests/AssertionsInTestsCheck/SpringUtilAssertTestSample.java"))
133+
.withCheck(check)
134+
.verifyIssues();
135+
}
136+
129137
}

0 commit comments

Comments
 (0)