google / google/error-prone

`DirectInvocationOnMock` doesn't understand `MockedStatic` with `@Mock`

Open
#4,039 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
7.2k
Forks
820
Avg merge
5h 9m
Merged PRs (30d)
50

Description

The `DirectInvocationOnMock` inspection warns when calling methods on [`MockedStatic`](https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/MockedStatic.html) objects such as `when` or `verify`, if the `MockedStatic` is declared using [`@Mock`](https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mock.html).

Example:

```java
import java.net.http.HttpClient;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class ExampleTest {
@Mock
HttpClient mockedClient;

@Mock
MockedStatic staticHttpClient;

@BeforeEach
void setup() {
this.staticHttpClient.when(HttpClient::newHttpClient).thenReturn(this.mockedClient);
}
}
```

This code snippet will generate a `DirectInvocationOnMock` warning for the `staticHttpClient.when()` call, but that is correct usage of the API.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.