Notes
Java Testing
Java Testing
  • Java Testing
  • Mockito
    • Mockito.mock()
    • Mockito.spy()
    • Mockito.verify()
    • Mockito.mockStatic()
    • ArgumentMatcher
    • ArgumentCaptor
  • PowerMockito
    • Why we need PowerMock?
    • PowerMockRunner
    • PrepareForTest
      • Understanding @PrepareForTest
    • SuppressStaticInitializationFor
    • PowerMockito.suppress()
    • PowerMockito.stub()
    • PowerMockito.replace()
    • PowerMockito.whenNew()
      • Limitation of PowerMockito.whenNew()
    • Whitebox.invokeMethod()
    • Whitebox.setInternalState()
    • Whitebox.getInternalState()
  • Tips
    • How to test ViewModel and LiveData
Powered by GitBook
On this page
  1. Mockito

Mockito.mockStatic()

Mock static methods without PowerMock

Now with Mockito 3, you can mock Classes with static methods.

Here is an example:

try (MockedStatic<Foo> dummyStatic = Mockito.mockStatic(ClassName.class)) {
    dummyStatic.when(() -> ClassName.method("param1"))
               .thenReturn("someValue");
    // when
    System.out.println(ClassName.method("param1"));
    //then
    dummyStatic.verify(
            () -> ClassName.method("param1"),
            times(1), 
    );
}
PreviousMockito.verify()NextArgumentMatcher

Last updated 2 years ago