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.

Update OngoingStubbing to take a Supplier<T>

See original GitHub issue

Currently the OngoingStubbing interface only supports single values or varargs as part of the thenReturn() method. This method should be overloaded to include thenReturn(Supplier<T>). This feature will users testing streaming processes by being able to have new objects generated for each iteration through their pipeline.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:7
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
ZeLarpMastercommented, Oct 29, 2018

Until it’s changed, because Answer is a functional interface, you can use thenAnswer and then like this:

private MyClass mockedClassInstance;
Supplier<Integer> intSupplier = () -> Random.nextInt(100);
when(mockedClassInstance.getInt()).then(i -> intSupplier.get())

Or

Function<?, Integer> answer = i -> Random.nextInt(100);
when(mockedClassInstance.getInt()).then(answer);

Or

when(mockedClassInstance.getInt()).then(i -> Random.nextInt(100));
0reactions
stephan-henningsencommented, Jul 18, 2020

The latter of @ZeLarpMaster 's snippets helped out with the doAnswer() variant for spies. I completely forgot about the Answer interface, but it does the job here as well:

doAnswer( answer -> Random.nextInt(100) ).when(spy).getNiceInt();
Read more comments on GitHub >

github_iconTop Results From Across the Web

Java Generics - method not applicable to Mockito generated ...
static <T> OngoingStubbing<T> whenX( Supplier<T> sup ) { return Mockito.when( sup.get() ); } whenX(thingsProvider::getThings).
Read more >
OngoingStubbing (Mockito 2.2.9 API) - javadoc.io
Sets a generic Answer for the method. Sets the real implementation to be called when the method is called on a mock object....
Read more >
JUnit 5 tutorial, part 1: Unit testing with JUnit 5, Mockito, and ...
Introduction to Mock objects using Mockito. Thus far we have only reviewed testing simple methods that do not rely on external dependencies, but ......
Read more >
Implementing Mockito from Scratch
When running the class, the test succeeds. The goal is now to remove all Mockito import statements and to rewrite the minimal code...
Read more >
Diff - platform/external/mockito - Google Git
-9,4 +9,4 @@ The source can be updated using the update_source.sh script. ... For partial mock it's recommended to use <code>doReturn</code> syntax.
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