mirror of
https://github.com/github/codeql.git
synced 2026-04-29 02:35:15 +02:00
Java: add single-method class test case for mocking rule
Classes with only one public method should be compliant when mocked.
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* Simple class with a single public method to test the edge case.
|
||||
* When this single method is mocked, it means ALL public methods are mocked.
|
||||
*/
|
||||
public class EmployeeStatus {
|
||||
public String getStatus() {
|
||||
return "active";
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,3 @@
|
||||
| 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 |
|
||||
| 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 |
|
||||
| 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 |
|
||||
|
||||
@@ -52,4 +52,14 @@ public class TestORM {
|
||||
doReturn(0).when(employeeRecordMock).update(sampleEmployee, "Jane Doe"); // Mocked EmployeeRecord.update
|
||||
doReturn(0).when(employeeRecordMock).delete(sampleEmployee); // Mocked EmployeeRecord.delete
|
||||
}
|
||||
|
||||
/**
|
||||
* Edge case: Class with single public method - should NOT be flagged.
|
||||
* When there's only one public method, mocking it doesn't indicate a "too big" test.
|
||||
*/
|
||||
@Test
|
||||
public void compliantSingleMethod() { // $ SPURIOUS: Alert
|
||||
EmployeeStatus statusMock = mock(EmployeeStatus.class); // COMPLIANT: Single public method, no choice but to mock it if needed
|
||||
when(statusMock.getStatus()).thenReturn("inactive"); // Mocked EmployeeStatus.getStatus (the only public method, but that's OK)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user