1

I have a repository class something like ...

public class StuffRepository {
    public Stuff save(Stuff v);
}

I want to make a mock with @MockBean returning first parameter v. How can I do that?

@MockBean
private StuffRepository stuffRepository;

public void test() {
    given(stuffRepository.save(??)).willReturn(??);
}

2 Answers 2

3

You use the Answer mocking version.

given(stuffRepository.save(any())).will(i -> {
    return i.getArgument(0);
});
3

Mock upStuff and have that be the return value.

@MockBean
private StuffRepository stuffRepository;
@Mock
private Stuff v;

public void test() {
    given(stuffRepository.save(v)).willReturn(v);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.