For example: public interface MyInterface { public String getName(); }public class B implements MyInterface { ... }public class BB implements MyInterface { ... } 在A里: MyInterface b = new BB(); -> 这里用BB替代B。 这是最常见的方式。
mockito http://mockito.org/ //You can mock concrete classes, not only interfaces LinkedList mockedList = mock(LinkedList.class);
public interface MyInterface {
public String getName();
}public class B implements MyInterface {
...
}public class BB implements MyInterface {
...
}
在A里:
MyInterface b = new BB(); -> 这里用BB替代B。
这是最常见的方式。
LinkedList mockedList = mock(LinkedList.class);
//stubbing
when(mockedList.get(0)).thenReturn("first");
when(mockedList.get(1)).thenThrow(new RuntimeException());