Skip to content

Instantly share code, notes, and snippets.

@kevinhooke
Last active April 24, 2025 10:42
Show Gist options
  • Save kevinhooke/da53ba36f9e3755a664097236c96b74b to your computer and use it in GitHub Desktop.
Save kevinhooke/da53ba36f9e3755a664097236c96b74b to your computer and use it in GitHub Desktop.
Mocking static methods with Mockito
# No longer need to use Powermock to mock statics, this is now supported in Mockito with .mockStatic()
# Alternative approach to https://gist.github.com/kevinhooke/b4035faa5f2c215e8166936a44db4fa3
# Requires mockito-inline dependency instead of mockito-core
try (MockedStatic<ClassToMock> mock = Mockito.mockStatic(ClassToMock.class)) {
mock.when(ClassToMock::staticMethod).thenReturn(mockedReturn);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment