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:
Napalys Klicius
2025-08-11 11:23:01 +02:00
parent 22caa584ad
commit a9e9a62439
3 changed files with 20 additions and 0 deletions

View File

@@ -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";
}
}

View File

@@ -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 |

View File

@@ -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)
}
}