简体   繁体   中英

How can I mock bean returning its own parameter?

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(??);
}

Mock up Stuff and have that be the return value.

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

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

You use the Answer mocking version.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM