I am writing unit tests with JMock and I have a mocked out void method that updates a list that is passed in as a parameter. Unfortunately, this list is internal to the method I am testing so I have no reference to it. Is it possible to get it from the expectation created? I need to be able to insert objects into the list to mimic the behavior of the mocked class. The alternative route will be to pass the list along as the return type rather than as a parameter; this is probably better practice too.
context.checking(new Expectations() {
{
// need to reference this list to insert objects into
oneOf(mockedClass).mockedMethod(with(any(List.class)));
}
});
You can access the method's input via a custom matcher, but it carries with it a bad smell as you would be manipulating the structure inside the matcher which is not its purpose.
A better route would be to change the design of the method to return the list so you can mock it out appropriately.