// Create a mock object of a Calculator interface Calculator mockCalculator = EasyMock.createMock(Calculator.class); // Set up expectations for a method call on the mock object EasyMock.expect(mockCalculator.add(2, 3)).andReturn(5); // Enter the replay state for the mock object EasyMock.replayAll(mockCalculator); // Use the mock object in a test case assertEquals(5, mockCalculator.add(2, 3));
// Create a mock object of a UserRepository interface UserRepository mockRepository = EasyMock.createMock(UserRepository.class); // Set up expectations for a method call on the mock object EasyMock.expect(mockRepository.updateUser(123, "John")).andReturn(true); // Enter the replay state for the mock object EasyMock.replayAll(mockRepository); // Use the mock object in a test case assertTrue(mockRepository.updateUser(123, "John"));In this example, we create a mock object of the UserRepository interface and set up an expectation for the updateUser method. We then use the replayAll method to enter the replay state for the mock object and use it in a test case. The org.easymock package library is commonly used for creating mock objects in unit tests.