Wednesday, April 4, 2018

Mockito and PowerMock Framework for JUNIT testing!

In this blog, i would like to share some of the important concepts in the Mockito/Powermock framework.

1. doReturn/when
2. eq
3. @InjectMocks
4 @Mock
5. @Runwith PowerMockRunner and Mockito
6. PowerMockito.doReturn(output).when(spyCbvApi,MemberMatcher.method(RestServices_CBV_APImpl.class,"getCVTDecision",GetCVTDecisionInput.class,boolean.class)).withArguments(any(GetCVTDecisionInput.class), Matchers.anyBoolean());
7. Naming convention for JUnit test cases.
8. whitebox
mocking static methods, variables
9. argumentcaptor
10. Mocking a static method:

https://stackoverflow.com/a/21116014/1739949
@RunWith(PowerMockRunner.class)
@PrepareForTest(DriverManager.class)
public class Mocker {

    @Test
    public void testName() throws Exception {

        //given
        PowerMockito.mockStatic(DriverManager.class);
        BDDMockito.given(DriverManager.getConnection(...)).willReturn(...);

        //when
        sut.execute();

        //then
        PowerMockito.verifyStatic();
        DriverManager.getConnection(...);

    }