@Test public void testAddition() { // Create a mock object for a Calculator Calculator mockCalc = mock(Calculator.class); // Create a Capture object for the output of add method CapturecaptureOutput = EasyMock.newCapture(); // Expect a call to add method with arguments 5 and 10 expect(mockCalc.add(5, 10)).andReturn(15); // Capture the output of the method call mockCalc.add(EasyMock.anyInt(), EasyMock.capture(captureOutput)); expectLastCall().anyTimes(); // Replay the mock object replay(mockCalc); // Call the method to be tested with arguments 5 and 10 int result = myMethodUnderTest(mockCalc); // Verify the mock object verify(mockCalc); // Assert that the output of the mock method call was 15 assertEquals(captureOutput.getValue(), Integer.valueOf(15)); }
@Test public void testRestAPI() { // Create a mock object for a REST API client RestClient mockClient = mock(RestClient.class); // Create a Capture object for the request payload CaptureIn this example, we are testing a method that makes a REST API call using a RestClient mock object. We are using EasyMock capture to capture the payload of the API request, which is expected to be "test". We are then verifying that the payload captured was indeed "test".captureRequest = EasyMock.newCapture(); // Expect a call to the REST API with a payload containing "test" expect(mockClient.sendRequest(EasyMock.capture(captureRequest))).andReturn("success"); // Replay the mock object replay(mockClient); // Call the method to be tested with payload "test" String result = myMethodUnderTest(mockClient, "test"); // Verify the mock object verify(mockClient); // Assert that the payload captured was "test" assertEquals(captureRequest.getValue(), "test"); }