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.

How to capture and return value?

See original GitHub issue

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

github_iconTop GitHub Comments

1reaction
NagRockcommented, Apr 28, 2017

Should be fixed in 2.0 version. Please reopen if something is still wrong.

1reaction
NagRockcommented, Mar 28, 2017

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");

Read more comments on GitHub >

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

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