Making mocks easier to write

Though my association with Java is quite long back ( since 1999), I recently stated working on creating Android Applications .

One of the important part of software development is that the code should be testable in Unit. That means mock out whatever is external to the code and just check whether the call to methods behaves in expected ways .

In Mockito I used to mock the members with a method


NetworkApi networkApiMock = Mockito.mock(NetworkApi.class);


when you import static mock method mock by doing this



import static org.mockito.Mockito.mock;

Yet better way is when you can do things like


@Mock
 NetworkApi networkApiMock;

This will work when the class under test is annotated with


@RunWith(MockitoJUnitRunner.class)
 Class ClassUnderTest{

@Mock
 NetworkApi networkApiMock;

@Test
 public void testCase(){

when(networkApiMock.getUri()).thenReturn();

}

 

Quite handy and saves time 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *