使用 Mockito 2.0.7 模拟 lambda 表达式

Use Mockito 2.0.7 to mock lambda expressions(使用 Mockito 2.0.7 模拟 lambda 表达式)
本文介绍了使用 Mockito 2.0.7 模拟 lambda 表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想像这样模拟我的存储库中提供的查询:

I want to mock a query provided on my repository like this:

@Test
public void GetByEmailSuccessful() {
    // setup mocks
    Mockito.when(this.personRepo.findAll()
            .stream()
            .filter(p -> (p.getEmail().equals(Mockito.any(String.class))))
            .findFirst()
            .get())
            .thenReturn(this.personOut);
    Mockito.when(this.communityUserRepo.findOne(this.communityUserId))
            .thenReturn(this.communityUserOut);
...

我的 @Before 方法如下所示:

My @Before method looks like this:

@Before
public void initializeMocks() throws Exception {
    // prepare test data.
    this.PrepareTestData();

    // init mocked repos.
    this.personRepo = Mockito.mock(IPersonRepository.class);
    this.communityUserRepo = Mockito.mock(ICommunityUserRepository.class);
    this.userProfileRepo = Mockito.mock(IUserProfileRepository.class);
}

不幸的是,当我运行测试时,我收到了错误:

Sadly when I run the test I receive the error:

java.util.NoSuchElementException:不存在值

java.util.NoSuchElementException: No value present

当我双击错误时,它指向第一个 lambda 的 .get() 方法.

When I double-click the error it points at the .get() method of the first lambda.

你们中是否有人成功地模拟了一个 lambda 表达式并知道如何解决我的问题?

Have any of you successfully mocked a lambda expression and know how I can solve my problem?

推荐答案

没有必要模拟这么深的调用.只需模拟 personRepo.findAll() 并让 Streaming API 正常工作:

There's no need to mock such deep calls. Simply mock personRepo.findAll() and let the Streaming API work as normal:

Person person1 = ...
Person person2 = ...
Person person3 = ...
List<Person> people = Arrays.asList(person1, person2, ...);
when(personRepo.findAll()).thenReturn(people);

然后代替

.filter(p -> (p.getEmail().equals(Mockito.any(String.class))))

只需将 Person 对象上的 email 设置/模拟为预期值.

just set/mock email on your Person objects to be the expected value.

或者,考虑实现 PersonRepo.findByEmail.

这篇关于使用 Mockito 2.0.7 模拟 lambda 表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Sending a keyboard event from java to any application (on-screen-keyboard)(将键盘事件从 java 发送到任何应用程序(屏幕键盘))
How to make JComboBox selected item not changed when scrolling through its popuplist using keyboard(使用键盘滚动其弹出列表时如何使 JComboBox 所选项目不更改)
Capturing keystrokes without focus(在没有焦点的情况下捕获击键)
How can I position a layout right above the android on-screen keyboard?(如何将布局放置在 android 屏幕键盘的正上方?)
How to check for key being held down on startup in Java(如何检查在Java中启动时按住的键)
Android - Get keyboard key press(Android - 获取键盘按键)