A mocking method that returns the page interface

I have a method that I need to write unit test. The method returns a type Page.

How can I mock this method?

Method:

public Page<Company> findAllCompany( final Pageable pageable )
{
    return companyRepository.findAllByIsActiveTrue(pageable);
}

thanks for the help

+4
source share
1 answer

You can use the answer Mockor the actual answer, and then use when, for example:

Page<Company> companies = Mockito.mock(Page.class);
Mockito.when(companyRepository.findAllByIsActiveTrue(pageable)).thenReturn(companies);

Or just instantiate the class:

List<Company> companies = new ArrayList<>();
Page<Company> pagedResponse = new PageImpl(companies);
Mockito.when(companyRepository.findAllByIsActiveTrue(pageable)).thenReturn(pagedResponse);
+7
source

Source: https://habr.com/ru/post/1681779/


All Articles