简体   繁体   中英

What can be unit tested in this Java method involving pagination of data?

Beginner here at unit testing. Following is the method to be tested -

public ListBeneficiaryResponseDTO getBeneficiaryOfMerchants(Long merchantId, Integer page, Integer pageSize,
                                                       String sortDirection, String sortField) {
        LOGGER.info("Inside getBeneficiaryOfMerchants method");

        // default we are setting to added on desc sort
        Sort sort = Sort.by(Sort.Direction.DESC,"addedOn");
        if(sortField != null && sortDirection != null) {
            sort = Sort.by(Sort.Direction.fromString(sortDirection),sortField);
        }

        Pageable pageRequest = PageRequest.of(page-1, pageSize, sort);
        Page<Beneficiary> pageOfBeneficiaries = beneficiaryRepository.findAllByMerchantId(merchantId, pageRequest);

        List<BeneficiaryResponseDTO> benResonseDtoList = new ArrayList<BeneficiaryResponseDTO>();

        for( Beneficiary ben: pageOfBeneficiaries.getContent()) {
            benResonseDtoList.add(this.getBeneficiaryResponseDTO(ben));
        }
        ListBeneficiaryResponseDTO formattedListBen = new ListBeneficiaryResponseDTO(pageOfBeneficiaries.getTotalPages(),pageOfBeneficiaries.getTotalElements(),pageOfBeneficiaries.getNumber(),benResonseDtoList);
        return formattedListBen;
    }

In order to write tests, I thought, what can this method do wrong, given the underlying method calls work fine. Well, I am not sure, but may be some error in putting the elements into the list.

So, I thought writing a test to ensure that the expected number of elements are present in the list benResonseDtoList.

Following is what I tried -

@Test
public void testGetBeneficiariesOfMerchant() throws Exception {

    Long merchantId = 2l;
    List<Beneficiary> beneficiaryList = new ArrayList<Beneficiary>();
    beneficiaryList.add(getBeneficiaryDto());
    beneficiaryList.add(getBeneficiaryDto());


    Page<Beneficiary> beneficiaries = new PageImpl<Beneficiary>(beneficiaryList); //But I am not sure how many entries are there in the page created.
Mockito.when(beneficiaryRepository.findAllByMerchantId(any(),any())).thenReturn(beneficiaries);
KeyManager keyManager = Mockito.mock(KeyManager.class);


ListBeneficiaryResponseDTO list = beneficiaryService.getBeneficiaryOfMerchants(merchantId,1,2, "DESC","addedOn");

If there were a clear correlation between the number of elements in beneficiaryList and the entries in pageOfBeneficiaries, I could test that.

If I understand your question correctly you want to know the testing scenarios for getBeneficiaryOfMerchants() function given underlining functions are working correctly.

For a given

merchantId, page, pageSize, sortDirection, sortField

ListBeneficiaryResponseDTO would be fixed, let's call this ListBeneficiaryResponseDTO expectedList (you have to construct it as you already know the correct output)

So you should be comparing the following:-

Actual Output:

ListBeneficiaryResponseDTO actualList =  beneficiaryService.getBeneficiaryOfMerchants(merchantId,1,2, "DESC","addedOn");

Expected Output: ListBeneficiaryResponseDTO expectedList(already defined above)

Comparing both the outputs:-

  1. Compare field by field actualList & expectedList

  2. Override the Equals method of the ListBeneficiaryResponseDTO class(or you can use lombok.EqualsAndHashCode to avoid boilerplate code) and do the following:- assertEquals(**actualList**, **expectedList**)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM