【发布时间】:2015-05-06 15:35:28
【问题描述】:
我想像这样模拟我的存储库中提供的查询:
@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 方法如下所示:
@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);
}
不幸的是,当我运行测试时,我收到了错误:
java.util.NoSuchElementException:不存在值
当我双击错误时,它指向第一个 lambda 的 .get() 方法。
你们中有人成功地模拟了一个 lambda 表达式并且知道我该如何解决我的问题吗?
【问题讨论】:
-
我可能错了,但我认为您必须先为
personRepo.findAll()指定返回值,然后再为所有方法调用指定返回值。 -
我有一种不好的感觉,你想测试 mockito 而不是测试你的代码。 getByEmail() 的代码是什么?它应该怎么做?
标签: java unit-testing junit lambda mockito