I would like to unit test a method with multiple internal calls to a class I want to mock using EasyMock.
The test method actually runs 5 times and calls the mocked method.
During each loop, I will create some objects, all of the same class (let's say of class A). The private method will call the mock object method that takes the instance of class A, evaluate it and return a result.
In the end, the public method will return a List of results.
I tried the standard EasyMock.expect(MockClass.method(A)).andReturn() but it does not work since there is no implementation of equals() for class A:
// this is the method example I am trying to test public methodToTest(){ // some logic privateMethodToTest(x); // some logic } private List<B> privateMethodToTest(int x){ List<B> list = new ArrayList<>(); List<A> all = getObjects(x); //getObjects private method for (A a:all){ list.add(objectToMock.methodToMock(a)); return list; } This is how I would like it to work:
EasyMock.createMock(ObjectToMock.class); EasyMock.expect(ObjectToMock.methodToMock(A)/* when A.getValue() == 1 */.andReturn("B object number 1") EasyMock.expect(ObjectToMock.methodToMock(A)/* when A.getValue() == 2 */.andReturn("B object number 2") //... and so on //object of class A does not implement equals() I am not sure how to do it and I was not able to find any similar example or answer to my question.
equalsimplementation to yourAclass? Seems like a best practice IMHO.Flexible Expectations with Argument Matchers; you can match yourAclass based on some other property (assuming it has any).