0

Can someone help me with how to write a Test Class for the below Wrapper Class...

    public class OppRelatedSolutionsController {
    public List<WrapperClass> wrapList{get;set;}
    public List<Solution__c> selectedRecords{get;set;}
    public OppRelatedSolutionsController(ApexPages.StandardController stdController) {
        wrapList = new List<WrapperClass>();
        for(Solution__c solRecs : [SELECT Name, Amount__c, Expected_Revenue__c FROM Solution__c WHERE Opportunity__c = NULL]) {
            wrapList.add(new WrapperClass(solRecs));
        }
        if(wrapList.size() <= 0) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'No Records found'));
        }
    }
    public pageReference saveRecords() {
        selectedRecords = new List<Solution__c>();
        for(WrapperClass wrapRecs : wrapList) {
            if(wrapRecs.selected == TRUE) {
                wrapRecs.sol.Opportunity__c = ApexPages.currentPage().getParameters().get('id');
                selectedRecords.add(wrapRecs.sol);
            }
        }
        update selectedRecords;
        if(selectedRecords.size() > 0) {
            pageReference ref = new PageReference('/'+ApexPages.currentPage().getParameters().get('id'));
            ref.setRedirect(true);
            return ref;
        }
        return null;
    }
    //Wrapper Class
    public class WrapperClass {
        public Solution__c sol{get;set;}
        public Boolean selected{get;set;}
        public WrapperClass(Solution__c s) {
            sol = s;
            selected = false;
        }
    }
}

I've gone through a few references and wrote Test Class but I was not able to cover all the code. Can someone help me with what needs to be changed in my test class.

@isTest
public class OppRelatedSolutionsControllerTest {
    public static testMethod void method1() {
        Solution__c solRec = new Solution__c();
        solRec.Name = 'Test Solution';
        solRec.Amount__c = 582;
        solRec.Expected_Revenue__c = 879;
        insert solRec;
        Test.startTest();
        List<WrapperClassTest> wrapList = new List<WrapperClassTest>();
        List<Solution__c> selectedRecords = new List<Solution__c>();
        for(Solution__c recs : [SELECT Name, Amount__c, Expected_Revenue__c FROM Solution__c WHERE Opportunity__c = NULL]) {
            wrapList.add(new WrapperClassTest(recs));
        }
        ApexPages.StandardController stdController = new ApexPages.StandardController(solRec);
        OppRelatedSolutionsController oppRel = new OppRelatedSolutionsController(stdController);
        oppRel.saveRecords();
        for(WrapperClassTest selWrap : wrapList) {
            if(selWrap.selected == TRUE) {
                selWrap.sol.Opportunity__c = ApexPages.currentPage().getParameters().get('id');
                selectedRecords.add(selWrap.sol);
            }
        }
        update selectedRecords;
        Test.stopTest();
    }
    public class WrapperClassTest {
        public Solution__c sol{get;set;}
        public Boolean selected{get;set;}
        public WrapperClassTest(Solution__c s) {
            sol =s;
            selected = false;
        }
    }
}

1 Answer 1

0

Can you please try the below test class

    @isTest
public class OppRelatedSolutionsControllerTest {
    public static testMethod void method1() {
        Solution__c solRec = new Solution__c();
        solRec.Name = 'Test Solution';
        solRec.Amount__c = 582;
        solRec.Expected_Revenue__c = 879;
        insert solRec;
        Test.startTest();
        ApexPages.StandardController stdController = new ApexPages.StandardController(solRec);
        OppRelatedSolutionsController oppRel = new OppRelatedSolutionsController(stdController);
        oppRel.saveRecords();
        Test.stopTest();
    }
}
2
  • Hi @Amrit Jain, The test is worked but there are few lines in my class which were not covered. Apex page messages and Page reference functions were not covered. Can you help me how to resolve this..
    – user93540
    Commented Mar 2, 2021 at 12:51
  • you can add on e more test case which will create a Solution record with Opportunity. This will make selectedRecords size 0 as Opportunity will not be null.
    – Amrit Jain
    Commented Mar 3, 2021 at 7:37

You must log in to answer this question.