Is there an already built-in, standard way in JMock to capture method arguments to test the argument object later on with standard JUnit functionality?
Something like
final CapturedContainer<SimpleMailMessage>capturedArgumentContainer = new ...
context.checking(new Expectations() {{
oneOf(emailService.getJavaMailSender()).send(
with(captureTo(capturedArgumentContainer)));
}});
assertEquals("helloWorld", capturedArgumentContainer.getItem().getBody());
CapturedContainer
and captureTo
do not exist — they are what I'm asking for.
Or do I need to implement this on my own?
You can do this by implementing a new Matcher that captures the argument when match is called. This can be retrieved later.
class CapturingMatcher<T> extends BaseMatcher<T> {
private final Matcher<T> baseMatcher;
private Object capturedArg;
public CapturingMatcher(Matcher<T> baseMatcher){
this.baseMatcher = baseMatcher;
}
public Object getCapturedArgument(){
return capturedArg;
}
public boolean matches(Object arg){
capturedArg = arg;
return baseMatcher.matches(arg);
}
public void describeTo(Description arg){
baseMatcher.describeTo(arg);
}
}
Then you can use this when setting up your expectations.
final CapturingMatcher<ComplexObject> captureMatcher
= new CapturingMatcher<ComplexObject>(Expectations.any(ComplexObject.class));
mockery.checking(new Expectations() {{
one(complexObjectUser).registerComplexity(with(captureMatcher));
}});
service.setComplexUser(complexObjectUser);
ComplexObject co =
(ComplexObject)captureMatcher.getCapturedArgument();
co.goGo();
I think you're missing the point a little here. The idea is to specify in the expectation what should happen, rather than capturing it and checking later. That would look like:
context.checking(new Expectations() {{
oneOf(emailService.getJavaMailSender()).send("hello world");
}});
or perhaps, for a looser condition,
context.checking(new Expectations() {{
oneOf(emailService.getJavaMailSender()).send(with(startsWith("hello world")));
}});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With