Posts

@Ignore

@Ignore is used to ignore execution of any test case. @Ignore public void test_Operation2(){ System.out.println("This test will no execute"); } This test will not execute.

Timeout Verification

@Test public void test_timeout(){ ArrayList<String> mockList =  Mockito.mock(ArrayList.class); mockList.add("1"); /* This test will pass if the .add("1") is called within 100 milliseconds */ Mockito.verify(mockList, Mockito.timeout(100)).add("1"); mockList.add("1"); /* This test will pass if the .add("1") is called two times within 100 milliseconds */ Mockito.verify(mockList, Mockito.timeout(100).times(2)).add("1"); }

Resetting mocks

@Test public void test_Resest(){ ArrayList mock = Mockito.mock(ArrayList.class); Mockito.when(mock.size()).thenReturn(10); mock.add(1); Mockito.reset(mock); //at this point the mock forgot any interactions & stubbing Mockito.verify(mock).add(1);  // here it will fail Assert.assertThat(mock.size(), CoreMatchers.is(10)); // here also it will fail }

@Test( expected = AnyException.class )

@Test( expected = ArithmeticException.class ) public void test_Expected_Exception_is_ArithmeticException(){ int number = 1 / 0; } Here this test must throw ArithmeticException, If it does not then test will fail. @Test( expected = IndexOutOfBoundsException.class ) public void test_Expected_Exception_is_IndexOutOfBoundException(){ (new ArrayList()).get(1); } Here this test must throw  IndexOutOfBoundsException , If it does not then test will fail. @Test( expected = ArrayIndexOutOfBoundsException.class ) public void test_Expected_Exception_is_ArrayIndexOutOfBoundException(){ int[] array =  new int[1]; int number = array[3]; } Here this test must throw  ArrayIndexOutOfBoundsException , If it does not then test will fail.

@InjectMocks Annotation

Example: class MyData{ private ArrayList<String> list; MyData(){ list = new ArrayList<String>(); list.add("Good Morning"); list.add("Good Noon"); list.add("Good Afternoon"); list.add("Good Evening"); list.add("Good Night"); } public String getValue(int index){ return list.get(index); } } public class InjectMockTestFlight { @Mock  ArrayList<String> list; @InjectMocks MyData myData; @Before public void init(){ MockitoAnnotations.initMocks(this); } @Test public void test_InjectMocks(){ Mockito.when(myData.getValue(0)).thenReturn("Good Morning"); } } It will inject the mock object into the @InjectMocks annotated object. In below example myData required the list object. myData annotated with the @InjectMocks and it will take the List @Mock annotated object.

Order Verification

It is used to verify the sequence of execution. Example: @Test public void test_Order(){ ArrayList<String> list = Mockito.mock(ArrayList.class); list.add("added first"); list.add("added second"); InOrder order = Mockito.inOrder(list); order.verify(list).add("added first"); order.verify(list).add("added second"); } Here it will verify .add() method first called with "added first" and  second time it was called with "added second" 

Mockito.doThrow()

Example: @Test public void test_doThrow(){ ArrayList  list = Mockito.mock(ArrayList.class); Mockito.doThrow(new Exception()).when(list).clear(); } Here it will throw Exception whenever you call list.clear() method