question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AdditionalAnswers Answer0 ?

See original GitHub issue

Hey,

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:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
goughy000commented, Jan 22, 2021

@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.

AtomicInteger i = new AtomicInteger();
when(mock.method()).thenAnswer(answer(() -> i.incrementAndGet()));

the only way to do this currently is like

AtomicInteger i = new AtomicInteger();
when(mock.method()).thenAnswer((Answer<Integer>) invocation -> i.incrementAndGet());

my actual use case where I came across this was something (but not exactly) like this

// given
CountDownLatch latch = new CountDownLatch(2);
when(mock.method()).thenAnswer((Answer<Integer>) invocation -> { latch.countDown(); return 1; });

// when
sut.call();

// then
latch.await(); // waits for 2 invocations on mock
assert(...);

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

when(mock.method()).thenAnswer(answer(() -> { latch.countDown(); return 1; }));

if method() was actual method(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?

when(mock.method(anyInt())).thenAnswer(answer(i -> { latch.countDown(); return 1; }));
1reaction
goughy000commented, Jan 22, 2021

image

when(mock.method()).thenAnswer(invocation -> i.incrementAndGet());

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found