0

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.

Andrii Liubimov
  • 497
  • 4
  • 12
  • Each instantiation of a mock-up class creates a new mocked implementation class for the interface, so yes, it's not a cheap operation to be done thousands of times. But is there really a need for this? In this case, it's easier to create a simple class implementing the interface. – Rogério Jun 23 '14 at 15:05
  • I am consider this way to go. However I was wondered if mocking can help. However if change interface then it would cause fake object change as well. It is not trivial when interface is managed by another team. Probably, then another team should manage their fake objects for testing purposes. – Andrii Liubimov Jun 30 '14 at 14:51

1 Answers1

0

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.

Andrii Liubimov
  • 497
  • 4
  • 12