如何获得模拟方法为任何参数返回不同的值(从数组)?

时间:2019-01-16 20:40:26

标签: java mocking mockito

我有一个findSubmission方法。想象一下,在数组提交中可能有返回值。现在,我将按他们的指数返回。但是我想一个接一个地返回提交的所有值,而不像现在这样使用索引。

        public function sponsoring() {
$user = User::find(Auth::user()->id);
return view('profile.sponsoring.index',['user' => Auth::user()]);
        }
// In View:
@foreach ($user->kids as $kids)
    <p>You are currently sponsoring {{ $kids->first_name }} {{ $kids->last_name }}</p>
@endforeach

1 个答案:

答案 0 :(得分:2)

您可以使用:

import org.mockito.AdditionalAnswers;

when(submissionService.findSubmission(Mockito.anyLong())).thenAnswer(AdditionalAnswers.returnsElementsOf(Arrays.asList(submission)));