Skip to content

Commit 7d60d91

Browse files
committed
Java: add single-method class test case for mocking rule
Classes with only one public method should be compliant when mocked.
1 parent 29b4466 commit 7d60d91

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Simple class with a single public method to test the edge case.
3+
* When this single method is mocked, it means ALL public methods are mocked.
4+
*/
5+
public class EmployeeStatus {
6+
public String getStatus() {
7+
return "active";
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
| TestORM.java:34:15:34:27 | nonCompliant1 | This test method mocks all public methods of a $@. | EmployeeRecord.java:4:14:4:27 | EmployeeRecord | class or an interface |
22
| TestORM.java:47:15:47:27 | nonCompliant2 | This test method mocks all public methods of a $@. | EmployeeRecord.java:4:14:4:27 | EmployeeRecord | class or an interface |
3+
| TestORM.java:61:15:61:35 | compliantSingleMethod | This test method mocks all public methods of a $@. | EmployeeStatus.java:5:14:5:27 | EmployeeStatus | class or an interface |

java/ql/test/query-tests/MockingAllNonPrivateMethodsMeansUnitTestIsTooBig/TestORM.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,14 @@ public void nonCompliant2() { // $ Alert
5252
doReturn(0).when(employeeRecordMock).update(sampleEmployee, "Jane Doe"); // Mocked EmployeeRecord.update
5353
doReturn(0).when(employeeRecordMock).delete(sampleEmployee); // Mocked EmployeeRecord.delete
5454
}
55+
56+
/**
57+
* Edge case: Class with single public method - should NOT be flagged.
58+
* When there's only one public method, mocking it doesn't indicate a "too big" test.
59+
*/
60+
@Test
61+
public void compliantSingleMethod() { // $ SPURIOUS: Alert
62+
EmployeeStatus statusMock = mock(EmployeeStatus.class); // COMPLIANT: Single public method, no choice but to mock it if needed
63+
when(statusMock.getStatus()).thenReturn("inactive"); // Mocked EmployeeStatus.getStatus (the only public method, but that's OK)
64+
}
5565
}

0 commit comments

Comments
 (0)