Skip to content

Commit d1f5600

Browse files
committed
Java: exclude single-method classes from mocking
1 parent 7d60d91 commit d1f5600

File tree

3 files changed

+3
-2
lines changed

3 files changed

+3
-2
lines changed

java/ql/src/Likely Bugs/Frameworks/JUnit/MockingAllNonPrivateMethodsMeansUnitTestIsTooBig.ql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ where
6262
exists(MockitoMockCall mockCall |
6363
mockCall.getParent+().(Stmt) = testMethod.getBody().getAStmt() and
6464
mockedClassOrInterface = mockCall.getMockedType() and
65+
// Only flag classes with multiple public methods (2 or more)
66+
count(Method m | m = mockedClassOrInterface.getAMethod() and m.isPublic()) > 1 and
6567
forex(Method method | method = mockedClassOrInterface.getAMethod() and method.isPublic() |
6668
exists(MockitoMockingMethodCall mockedMethod |
6769
mockedMethod.getMockitoMockCall() = mockCall and
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void nonCompliant2() { // $ Alert
5858
* When there's only one public method, mocking it doesn't indicate a "too big" test.
5959
*/
6060
@Test
61-
public void compliantSingleMethod() { // $ SPURIOUS: Alert
61+
public void compliantSingleMethod() {
6262
EmployeeStatus statusMock = mock(EmployeeStatus.class); // COMPLIANT: Single public method, no choice but to mock it if needed
6363
when(statusMock.getStatus()).thenReturn("inactive"); // Mocked EmployeeStatus.getStatus (the only public method, but that's OK)
6464
}

0 commit comments

Comments
 (0)