jeudi 28 juillet 2016

Unit testing with mockito for generic constructors

I have to test one method:

  @Override
    public PagedResult<AttributeForFilter> getInquiryAttributesForFilter(
            AttributeForFilterFilter filter, PagingParams pagedParams,
            SortFieldList<AttributeForFilterSortField> sortFields)
            throws GenericException {
        ...
        // a lot of code
        ...
        return new PagedResult<>(pagedParams.getPage(), attributes, totalCount);    
    }

I have tried to use this way, but it doesn't work:

PagedResult expected = mock(PagedResult.class);
whenNew(PagedResult.class).withArguments(page, attributes, 100L).thenReturn(expected);

PagedResult result = attrForFilterRepoBean.getInquiryAttributesForFilter(filter, pagedParams, sortFields);
assertSame("Should be same!", expected, result);

It's test output. Objects different, but, how we can see, args are the same:

junit.framework.AssertionFailedError: Should be same! expected same:<Mock for PagedResult, hashCode: 1461651289> was not:<PagedResult [page=page, totalCount=100, records=attributes]>
Expected :Mock for PagedResult, hashCode: 1461651289
Actual   :PagedResult [page=page, totalCount=100, records=attributes]

How to work with it? If i want to mock generic class constructor.

Aucun commentaire:

Enregistrer un commentaire