I have this service method that must be tested using JUnit:
@Override
public Page<WalletDTO> getWalletListByWalletTypes(List<String> walletTypesIds, Pageable pageable) throws NotFoundException {
if(walletTypesIds == null) {
Page<Wallet> walletsDTOList = this.walletRepository.findAll(pageable);
return walletsDTOList.map(m -> conversionService.convert(m, WalletDTO.class));
}
List<WalletType> walletsTypesEntity = new ArrayList<>();
for(String wt : walletTypesIds){
//WalletType wtEntity = this.walletTypeRepository.findByName(wt);
WalletType wtEntity = this.walletTypeRepository.findById(Integer.parseInt(wt)).get();
if(wtEntity == null) throw new NotFoundException("WalletType not found: " wt);
walletsTypesEntity.add(wtEntity);
}
Page<Wallet> walletsDTOList = this.walletRepository.findByWalletTypeIn(walletsTypesEntity,pageable);
return walletsDTOList.map(m -> conversionService.convert(m, WalletDTO.class));
}
As you can see it takes the Pageable pageable object as input parameter in order to page the result.
Then I created a JUnit test method like this:
@Test
@Order(2)
public void getWalletListByWalletTypesTest() throws NotFoundException {
List<String> singleItemList = Arrays.asList("1");
List<String> multiItemsList = Arrays.asList("1", "2", "3");
Page<WalletDTO> walletsType1DTOList = this.walletService.getWalletListByWalletTypes(singleItemList, null);
Page<WalletDTO> walletsAlltypesDTOList = this.walletService.getWalletListByWalletTypes(multiItemsList, null);
assertTrue(walletsType1DTOList.getNumberOfElements() > 0, "Exist at least one wallet");
assertTrue(walletsAlltypesDTOList.getNumberOfElements() > 0, "Exist at least one wallet");
assertTrue(walletsAlltypesDTOList.getNumberOfElements() < walletsType1DTOList.getNumberOfElements(),
"The wallets belonging to all the wallets types are > then the wallet belonging to a single wallet type");
}
But it is not working because when it call the getWalletListByWalletTypes() method to be tested it is passing null instead passing this Pageable parameter. So when the method is executed it thrown an exception when this line of code (when it call the repository method to retrieve data from DB) is executed:
Page<Wallet> walletsDTOList = this.walletRepository.findByWalletTypeIn(walletsTypesEntity,pageable);
it seems to me that I cannot create a Pageable object or I am not uderstending how.
What is the way to correctly test a method like this?
CodePudding user response:
There are two ways. Usually, for making unit test we use mock objects because Unit-test is a behavior test. Hence first way is just mocking Pageable class:
final Pageable sourcePageable = mock(Pageable.class);
and getting a size and a number of page by "when/then":
when(sourcePageable.getPageNumber()).thenReturn(pageNumber);
when(sourcePageable.getPageSize()).thenReturn(pageSize);
The second way as said Dirk Deyne above is creating a type which is implementing Pageable
interface(for example PageRequest
)
Which of them will you use it is up to you.
CodePudding user response:
for Unit Tests you can mock the dependencies which are out of scope of the tests.
I guess the most popular framework for this job in Java is Mockito