How to capture and return value?
See original GitHub issueI’m trying to do both a capture and return on the same mock, but I can’t figure out how to set it up. This is my attempt so far:
const mockedDS = mock(MyDataService);
const whenCall = when(mockedDS.update(anything(), anything());
whenCall.thenReturn("OK");
const firstCaptor = new Captor<Array<Employee>>();
const secondCaptor = new Captor<TimePeriod>();
whenCall.thenCapture(firstCaptor, secondCaptor);
It seems that I can only do one or the other? Depending on whether I call thenCapture
before thenReturn
or vice versa it will only capture OR return the value.
Btw. What I’d really like to write is this;
const mockedDS = mock(MyDataService);
const firstCaptor = new Captor<Array<Employee>>();
const secondCaptor = new Captor<TimePeriod>();
when(mockedDS.update(anything(), anything())
.thenCapture(firstCaptor, secondCaptor);
.thenReturn("OK");
…but both thenReturn
and thenCapture
returns void
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top Results From Across the Web
mockito: Is there a way of capturing the return value of stubbed ...
Specifically, given a spied object, I want to capture the return value. Based on Andreas_D's answer, here's what I came up with.
Read more >Solved: How to capture the return value of a method
Create a new Request Attribute and in the Data Source section, specify "Java Method Parameters". Once you've defined the class. method, you ...
Read more >Mockito ArgumentCaptor, @Captor Annotation - DigitalOcean
Mockito ArgumentCaptor is used to capture arguments for mocked methods. ... then getValue() method will return the latest captured value.
Read more >Capture method arguments and return value in Java - APM
I want to understand whether i can capture any method arguments or return value (of selected methods) without touching the source code?
Read more >Using Mockito ArgumentCaptor - Baeldung
2. Using ArgumentCaptor · 2.1. Set Up the Unit Test · 2.2. Add an ArgumentCaptor Field · 2.3. Capture the Argument · 2.4....
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 Free
Top 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
Should be fixed in 2.0 version. Please reopen if something is still wrong.
Thanks for appreciation. As I wrote earlier I will add functionality to capture and stub mock with API that you tried to use:
when(mock.smthn(anything())) .thenCapture(firstCaptor); .thenReturn("something");