// create a mock object for an interface MyInterface mock = EasyMock.createMock(MyInterface.class); // set an expectation for a method to be called once with a specific argument EasyMock.expect(mock.myMethod("Hello")).andReturn("World"); // activate the mock object EasyMock.replay(mock); // call the method on the mock object String result = mock.myMethod("Hello"); // verify that the method was called once with the specific argument EasyMock.verify(mock);
// create a mock object for an interface MyInterface mock = EasyMock.createMock(MyInterface.class); // set expectations for a method to be called multiple times with different arguments EasyMock.expect(mock.myMethod("apple")).andReturn("fruit"); EasyMock.expect(mock.myMethod("orange")).andReturn("fruit"); EasyMock.expect(mock.myMethod("carrot")).andReturn("vegetable"); // activate the mock object EasyMock.replay(mock); // call the method on the mock object multiple times with different arguments String result1 = mock.myMethod("apple"); String result2 = mock.myMethod("orange"); String result3 = mock.myMethod("carrot"); // verify that the method was called multiple times with the expected arguments EasyMock.verify(mock);
// create a mock object for an interface MyInterface mock = EasyMock.createMock(MyInterface.class); // set expectations for a method to return different results depending on the input EasyMock.expect(mock.myMethod(1)).andReturn("one"); EasyMock.expect(mock.myMethod(2)).andReturn("two"); EasyMock.expect(mock.myMethod(EasyMock.gt(2))).andReturn("many"); // activate the mock object EasyMock.replay(mock); // call the method on the mock object with different inputs String result1 = mock.myMethod(1); // returns "one" String result2 = mock.myMethod(2); // returns "two" String result3 = mock.myMethod(3); // returns "many" String result4 = mock.myMethod(4); // returns "many" // verify that the method was called with the expected inputs and returned the expected results EasyMock.verify(mock);