How to mock ERP system in JUnit
erp, java, junit, mocking
Solution
I assume you mean mock the Connection object? It's unclear if Connection is an interface or a class. Some mock object libraries only work on interfaces. Here are some of the more popular Java mock object libraries jmock, mockito and easymock
The basic idea would be to create a mock `Connection` object and have it return data that you want to test.
For example using easymock:
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);
Problem
I'm working on adding JUnit test to an enterprise solution's web services. My question is; how do I - if possible - mock the ERP system in the JUnit tests? For example I have a `getOrders(Connection con, String customerId)` method. It, however, makes one call to the ERP system to list all orders like: ``` public List<Order> getOrders(Connection con, String customerId) { // Call ERP system orders = con.fetchOrders(customerId); // Manipulate result orders... return orders; } ``` Any way to mock the ERP connection?