Search code examples
javamockingjmockit

List of MockUp instances


I am looking for the way to create list of mocked instances that returns different values, based on arguments provided in constructor.

public interface ValueObject {
    int getValueInt();
    String getValueString();
}

@RunWith(JMockit.class)
public class DemoTest {

    @Test
    public void testDemo() throws Exception {
        class ValueObjectMock extends MockUp<ValueObject> {
            private final int valueInt;
            private final String valueString;

            ValueObjectMock(int valueInt, String valueString) {
                this.valueInt = valueInt;
                this.valueString = valueString;
            }

            @Mock
            int getValueInt() {
                return valueInt;
            }


            @Mock
            String getValueString() {
                return valueString;
            }
        }


        final List<ValueObject> objects = new LinkedList<ValueObject>();

        for (int i = 0; i < 10000; i++) {
            objects.add(new ValueObjectMock(i, String.valueOf(i)).getMockInstance());
        }

        assertTrue(objects.get(5).getValueString().equals("5"));
    }
}

In this way test runs about 20 minutes. Is there is another way to create list of different mocks ?

PS: I am considering that I should use fake implementation for interface.

Solved by using fake objects implementing interface.


Solution

  • Instantiation of list of mocks is very hard operation. So better solution use fake objects that implement interface. However each time interface changes fake objects should be updated with interface.