Connection 객체를 모의한다고 생각하십니까? Connection이 인터페이스 또는 클래스인지 여부는 분명하지 않습니다. 일부 모의 객체 라이브러리는 인터페이스에서만 작동합니다. 여기에서 더 인기있는 자바 모의 객체 라이브러리 jmock, mockito 및 easymock
기본적인 아이디어는 모의 Connection
객체를 생성하고 그것을 테스트 할 데이터를 반환하는 것 중 일부입니다.
이
// create mock object (usually an instance member of your test)
@Mock Connection connection;
// initialize the mocks in the test setup
MockitoAnnotations.initMocks(this);
// create the thing you want returned by call to method
List<Order> orders = new ArrayList<Order>();
// specify what call to mock should do
when(connection.fetchOrders("custid")).thenReturn(orders);
Mockito만큼 그들이 마지막 아니에요으로 클래스를 조롱 수 있으며, 정적 메서드를 조롱 할 수 없습니다 Mockito
String customerId =...
List<Order> myOrders = ...
Connection mockConnection = EasyMock.createMock(Connection.class);
EasyMock.expect(mockConnection.fetchOrders(customerId)).andReturn(myOrders);
EasyMock.replay(mockConnection);
//call system under test:
List<Orders> results = getOrders(mockConnection, customerId);
List<Orders> expectedResults = ....
assertEquals(expectedResults, results);
감사합니다.이 기능은 구현에 가장 적합합니다. – Mike