Pagereference method :

public pagereference setPageParameters(){

    PageReference pageRef = Page.DTRC_Products;        

    string accountid = ApexPages.currentPage().getParameters().get('id');
    pageRef.getParameters().put('id', accountid);

    if(!String.isEmpty(SortBy) && SortBy.equals('asc')){
         pageRef.getParameters().put('SortBy', 'Desc');
    } else {
         pageRef.getParameters().put('SortBy', 'asc');
    return pageRef;


Comparable method :

public ProdWrapper(){
    public ProdWrapper(Integer AccountNo , String SortBy){
        this.AccountNo = AccountNo;
        this.SortBy = sortBy;

    public Integer compareTo(Object ObjToCompare) {

        ProdWrapper compareExpNum = (ProductWrapper)ObjToCompare;

        if (sortBy == 'desc'){
            if (eAccountNo == compareExpNum.AccountNo) return 0;
            if (AccountNo < compareExpNum.AccountNo) return 1;
            return -1;
        else if(sortBy == 'asc') {
            if (AccountNo == compareExpNum.AccountNo) return 0;
            if (AccountNo > compareExpNum.AccountNo) return 1;
            return -1;
        return 0;

Need some idea on how to cover the test class.

  • 2
    Have you started to create a test class? If so, editing your question to include it would be helpful. If you're new to unit testing and have no idea where to start, the Apex Testing module on Trailhead would be a good place to start.
    – Derek F
    Commented Feb 28, 2017 at 18:53

1 Answer 1


Hard to give you specifics when you have not even started/posted a test method nor the class name that implements the comparable interface

When you implement the test method and call .sort() on the class that implements the comparable interface the method will be covered (lines) but not tested (asserts).

You will need to assert the sorting is as expected using system asserts

Also, in your case you have some custom sort logic so you will need to set those properties sortBy for example and validate it is working as expected

Most likely you will end up with the following test methods

private static testmethod void sortDesc_eAccountNoFirst(){.....}

private static testmethod void sortDesc_eAccountNoLast(){.....}

private static testmethod void sortAsc_AccountNoFirst(){.....}

private static testmethod void sortAsc_AccountNoLast(){.....}

If you are new to testing visit TrailHead

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .