Equivalent of ArgumentCaptor when using mockConstruction

1,714 views
Skip to first unread message

Brandon Heck

unread,
Apr 21, 2022, 3:37:00 PM4/21/22
to mockito
Is there an equivalent of using an ArgumentCaptor when using inline constructor mocking? I have a method under test that instantiates some other objects with computed values and I would like to verify that those values are being computed correctly.

Brandon Heck

unread,
Apr 22, 2022, 1:45:18 AM4/22/22
to mockito
I found a way to achieve this, but it seems rather clunky:

...
final List<Object> constructorArgs = new ArrayList<>();
try(MockedConstruction mocks = mockConstruction(SomeClass.class, (mock, context) -> {
    constructorArgs.addAll(context.arguments());
})) {
  new SomeClass("parameter1", "parameter2");
  // iterate through constructorArgs performing assertions
}
...

Would the maintainers be open to adding first-class support somewhere in the API to get the constructor args used when the inline mock was injected?

Tim van der Lippe

unread,
Apr 22, 2022, 5:35:18 AM4/22/22
to mockito
This sounds like the intended pattern to me, and I don't think introducing a dedicated API for this is necessary. You can also put the assertions in the lambda directly, with the only caveat that it can't catch the case where the constructor isn't invoked at all.

Op vr 22 apr. 2022 om 07:45 schreef Brandon Heck <brando...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/609db10a-ed23-4fdc-bcf9-7e2cc97946cen%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages