Search code examples
javatestingjunitjmock

Standard way to capture arguments in JMock


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?


Solution

  • 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")));
    }});