AdditionalAnswers Answer0 ?
See original GitHub issueHey,
Firstly, thanks so much for mockito - probably my most liked and often use dependency, alongside junit and assertj, mockito is always in my “must haves” for any new project 😃
Feature Request:
Currently there are Answer1-Answer6 which can be passed to AdditionalAnswers.answer(…)
Would you be open to adding an Answer0 (and VoidAnswer0) ?
This would allow these uses cases
public interface MyInterface {
String method();
}
Currently have to cast, and have unused “invocation” input:
@Test
void test() {
MyInterface mock = mock(MyInterface.class);
when(mock.method()).thenAnswer((Answer<String>) invocation -> "hello");
}
With Answer0:
@Test
void test() {
MyInterface mock = mock(MyInterface.class);
when(mock.method()).thenAnswer(answer(() -> "hello"));
}
Also then allows for method ref:
@Test
void test() {
MyInterface mock = mock(MyInterface.class);
when(mock.method()).thenAnswer(answer(this::theAnswer));
}
private String theAnswer() {
System.out.println("we're in the answer!");
return "something";
}
(obviously these examples are contrived, imagine there being something other than being a basic return)
Happy to make a contribution for this if it’s something you agree could be useful?
Maybe there’s already a helper method in Mockito for this and I’ve just not found it…
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Introduction to Mockito's AdditionalAnswers | Baeldung
This method is mainly introduced to create a strongly typed answer in Java 8 using a functional interface. Typically, Mockito comes with a...
Read more >AdditionalAnswers (Mockito 3.1.0 API) - javadoc.io
Additional answers provides factory methods for answers. Currently offer answers that can return the parameter of an invocation at a certain position, ...
Read more >AdditionalAnswers (Mockito 2.2.7 API)
Additional answers provides factory methods for answers. Currently offer answers that can return the parameter of an invocation at a certain position, ...
Read more >Should I use doAnswer(AdditionalAnswers.returnsFirstArg ...
I have confused about using a proper way for testing ( employeeRepository.delete(employee) ) this method. I can use doAnswer(AdditionalAnswers.
Read more >Mockito 3 Additional Answers | wesome.org
... Mockito provides an AdditionalAnswers interface which provides a factory methods for answers. some of the methods provided by AdditionalAnswers are.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@TimvdLippe Yes I think you maybe missed something.
thenReturn only allows me to pass a precalculated value, not a value calculated at time of invocation.
e.g.
the only way to do this currently is like
my actual use case where I came across this was something (but not exactly) like this
given the Answer1-6 already existed, I felt it would have been convenient to write this if Answer0 had existed as I have no need for the InvocationOnMock parameter nor any method params
if
method()
was actualmethod(p)
then I can already do this in mockito… so it seems weird I can’t do it when there are no params, don’t you think?this doesn’t help with the method ref use case
I’ve added a helper method to my test class instead, thanks anyway for your consideration