mercredi 6 mai 2015

Use Mockito 2.0.7 to mock lambda expressions

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);
...

My @Befor 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: No value present

when i double click the error it points at the .get() method of the first lambda.

Does anyone of you successfully mocked a lamdba expression and knows how I can solve my problem?

Thx

Aucun commentaire:

Enregistrer un commentaire