简体   繁体   English

在这个涉及数据分页的 Java 方法中可以对什么进行单元测试?

[英]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.因此,我想编写一个测试来确保列表 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.如果 beneficiaryList 中的元素数量与 pageOfBeneficiaries 中的条目之间存在明显的相关性,我可以对其进行测试。

If I understand your question correctly you want to know the testing scenarios for getBeneficiaryOfMerchants() function given underlining functions are working correctly.如果我正确理解您的问题,您想知道getBeneficiaryOfMerchants() function 的测试场景,因为下划线功能正常工作。

For a given对于给定的

merchantId, page, pageSize, sortDirection, sortField MercerId, 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) ListBeneficiaryResponseDTO 将被修复,我们称之为 ListBeneficiaryResponseDTO expectedList (您必须构建它,因为您已经知道正确的输出)

So you should be comparing the following:-因此,您应该比较以下内容:-

Actual Output:实际 Output:

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

Expected Output: ListBeneficiaryResponseDTO expectedList(already defined above)预期 Output: ListBeneficiaryResponseDTO 预期列表(已在上面定义)

Comparing both the outputs:-比较两个输出: -

  1. Compare field by field actualList & expectedList逐个字段比较actualListexpectedList

  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**)覆盖 ListBeneficiaryResponseDTO 类的 Equals 方法(或者您可以使用 lombok.EqualsAndHashCode 来避免样板代码)并执行以下操作:- assertEquals(**actualList**, **expectedList**)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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