//You can mock concrete classes, not just interfaces git LinkedList mockedList = mock(LinkedList. class ); github //stubbing ide when(mockedList.get( 0 )).thenReturn( "first" ); 函數 when(mockedList.get( 1 )).thenThrow( new RuntimeException()); 測試 //following prints "first" spa System.out.println(mockedList.get( 0 )); code //following throws runtime exception 對象 System.out.println(mockedList.get( 1 )); ci //following prints "null" because get(999) was not stubbed System.out.println(mockedList.get( 999 )); //Although it is possible to verify a stubbed invocation, usually it's just redundant //If your code cares what get(0) returns, then something else breaks (often even before verify() gets executed). //If your code doesn't care what get(0) returns, then it should not be stubbed. verify(mockedList).get( 0 ); |