Skip to content

Commit b56f8cc

Browse files
committed
Java: Fix QLDoc style compliance and qhelp for mocking query
1 parent 53ccc56 commit b56f8cc

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
# J-T-001: Mocking all non-private methods of a class may indicate the unit test is testing too much
2-
3-
Mocking too many non-private methods of a class may indicate that the test is too complicated, possibly because it is trying to test multiple things at once.
4-
51
## Overview
62

7-
Mocking methods of a class is necessary for a unit test to run without overhead caused by expensive I/O operations necessary to compute their values. However, if a unit test ends up mocking all of them, it is likely a signal that the scope of the unit test is reaching beyond a single unit of functionality.
3+
Mocking methods of a class is necessary for unit tests to run without overhead caused by expensive I/O operations. However, when a unit test ends up mocking all non-private methods of a class, it may indicate that the test is too complicated, possibly because it is trying to test multiple things at once. Such extensive mocking is likely a signal that the scope of the unit test is reaching beyond a single unit of functionality.
84

95
## Recommendation
106

11-
It is best to contain the scope of a single unit test to a single unit of functionality. For example, a unit test may aim to test a series of data-transforming functions that depends on an ORM class. Even though the functions may be semantically related with one another, it is better to create a unit test for each function.
7+
It is best to contain the scope of a single unit test to a single unit of functionality. For example, a unit test may aim to test a series of data-transforming functions that depend on an ORM class. Even though the functions may be semantically related with one another, it is better to create a unit test for each function.
128

139
## Example
1410

@@ -30,14 +26,14 @@ public class TestORM {
3026
public void nonCompliant() {
3127
Employee sampleEmployee = new Employee("John Doe");
3228
EmployeeRecord employeeRecordMock = mock(EmployeeRecord.class); // NON_COMPLIANT: Mocked class has all of its public methods used in the test
33-
when(employeeRecordMock.add(Employee.class)).thenReturn(0); // Mocked EmployeeRecord.add
34-
when(employeeRecordMock.get(String.class)).thenReturn(sampleEmployee); // Mocked EmployeeRecord.get
35-
when(employeeRecordMock.update(Employee.class, String.class)).thenReturn(0); // Mocked EmployeeRecord.update
36-
when(employeeRecordMock.delete(Employee.class)).thenReturn(0); // Mocked EmployeeRecord.delete
29+
when(employeeRecordMock.add(sampleEmployee)).thenReturn(0); // Mocked EmployeeRecord.add
30+
when(employeeRecordMock.get("John Doe")).thenReturn(sampleEmployee); // Mocked EmployeeRecord.get
31+
when(employeeRecordMock.update(sampleEmployee, "Jane Doe")).thenReturn(0); // Mocked EmployeeRecord.update
32+
when(employeeRecordMock.delete(sampleEmployee)).thenReturn(0); // Mocked EmployeeRecord.delete
3733
}
3834

3935
@Test
40-
public void compliant1() {
36+
public void compliant() {
4137
Employee sampleEmployee = new Employee("John Doe");
4238
EmployeeRecord employeeRecordMock = mock(EmployeeRecord.class); // COMPLIANT: Only some of the public methods belonging to the mocked object are used
4339
when(employeeRecordMock.add(sampleEmployee)).thenReturn(0); // Mocked EmployeeRecord.add

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @id java/mocking-all-non-private-methods-means-unit-test-is-too-big
3-
* @name J-T-001: Mocking all non-private methods of a class may indicate the unit test is testing too much
3+
* @name Mocking all non-private methods of a class may indicate the unit test is testing too much
44
* @description Mocking all non-private methods provided by a class might indicate the unit test
55
* aims to test too many things.
66
* @kind problem
@@ -12,12 +12,15 @@
1212

1313
import java
1414

15+
/**
16+
* A call to Mockito's `mock` method.
17+
*/
1518
class MockitoMockCall extends MethodCall {
1619
MockitoMockCall() { this.getMethod().hasQualifiedName("org.mockito", "Mockito", "mock") }
1720

1821
/**
19-
* Gets the type that this call intends to mock. e.g.
20-
* ``` java
22+
* Gets the type that this call intends to mock. For example:
23+
* ```java
2124
* EmployeeRecord employeeRecordMock = mock(EmployeeRecord.class);
2225
* ```
2326
* This predicate gets the class `EmployeeRecord` in the above example.
@@ -26,8 +29,8 @@ class MockitoMockCall extends MethodCall {
2629
}
2730

2831
/**
29-
* A method call that mocks a target method in a JUnit test. e.g.
30-
* ``` java
32+
* A method call that mocks a target method in a JUnit test. For example:
33+
* ```java
3134
* EmployeeRecord employeeRecordMock = mock(EmployeeRecord.class);
3235
* when(employeeRecordMock.add(sampleEmployee)).thenReturn(0); // Mocked EmployeeRecord.add
3336
* doReturn(0).when(employeeRecordMock).add(sampleEmployee); // Mocked EmployeeRecord.add

0 commit comments

Comments
 (0)