import static org.easymock.EasyMock.expectLastCall; ListmockList = createMock(List.class); mockList.add("test"); expectLastCall().once(); replay(mockList); // This will invoke the method call on the mock object mockList.add("test"); // This will verify that the expectation was met verify(mockList);
import static org.easymock.EasyMock.expectLastCall; Calculator mockCalculator = createMock(Calculator.class); mockCalculator.divide(10, 0); expectLastCall().andThrow(new ArithmeticException("Division by zero")); // This will invoke the method call on the mock object mockCalculator.divide(10, 0); // This will throw an ArithmeticException with the message "Division by zero" replay(mockCalculator);This example sets up an expectation that the divide method on the mockCalculator object will be called with the arguments 10 and 0, and will throw an ArithmeticException with the message "Division by zero". The replay method is used to activate the mock object and set up the expectation. In both examples, the expectLastCall method is used to specify the expectation for the last method call made on the mock object.