Skip to main content
Hemanth P (CRMIT Solutions) 님이 #Apex에 질문했습니다
Hi all, I am new to Apex Unit-Testing and i have done some process . So, i have a Wrapper class inside Apex class. I got code coverage upto 55%. I dont know where i am getting wrong in this.                         

Apex Class:                                                         

 public with sharing class AccountRelatedListController {

    public static List<DataTableWrapper> getAllOpps(Id accId) {

        List<Opportunity> listOpp = [SELECT Id, Name ,StageName, CloseDate FROM Opportunity where AccountId =:accId ];

        List<DataTableWrapper> response = new List<DataTableWrapper>();

        for(Opportunity opp : listOpp){

            DataTableWrapper obj = new DataTableWrapper();

            obj.oppId = opp.Id;

            obj.name = opp.Name;

            obj.nameUrl = '/'+opp.Id;

            obj.stageName = opp.StageName;

            obj.closeDate = opp.CloseDate;

            response.add(obj);

        }

        return response;

    }

    private class DataTableWrapper {

        

        public Id oppId {get;set;}

        

        public String name {get;set;}

        

        public String nameUrl {get;set;}

        

        public String stageName {get;set;}

        

        public Date closeDate {get;set;}

    }

}                                                                                                                                Test Class :                                                                                     @isTest

public class TestAccountRelatedlistController {

    @testSetup static void setup() {

        

        //Create Account test record

        Account acc = TestDataFactory.createAccount(Name = 'Test');

        insert acc;

        System.AssertEquals(

            acc!=null,

            true,

            'Account record was not created'

        );

        

        //Create Contact test record

        Contact contactRecord = TestDataFactory.createContact(

            Firstname = 'Test', LastName = 'One'

        );

        

        insert contactRecord;

        System.AssertEquals(

            contactRecord!=null,

            true,

            'Contact record was not created'

        );

       

            Opportunity opp = TestDataFactory.createOpportunity(

            Name = 'OppTest', StageName = 'Prospecting', Closedate = DateTime.now()

        );

        opp.AccountId = acc.Id;

        insert opp;

        System.AssertEquals(

            opp!=null,

            true,

            'Opportunity record was not created'

        );

    }

    static testMethod void getallOpsTest()

    {

        

        Account a = [Select Id from Account Limit 1];

        Contact c = [Select Id,FirstName,Lastname from Contact LIMIT 1];

        List<Opportunity> op = [SELECT Id, Name ,StageName, CloseDate  FROM Opportunity where AccountId =:accId ];

        insert op;

       //  AccountRelatedListController obj = new AccountRelatedListController();

        

        AccountRelatedListController.DataTableWrapper wrapper = new AccountRelatedListController.DataTableWrapper();

        wrapper.StageName = 'Prospecting';

        wrapper.CloseDate = Datetime.now();

        wrapper.name = 'Test';

        wrapper.oppId = '08p920000000B57AEE';

        wrapper.nameUrl = 'Test';

        Test.startTest();

         AccountRelatedListController.getAllOpps(a.Id);

       Test.stopTest();

   

    }    

}                                                                                                               

Can anyone help me the code coverage for this Wrapper class. Thanks in advance !

 
답변 3개
  1. 2022년 3월 4일 오후 7:08
    Hi Jay Chandu,

    Try with below code.. you will get the 100% coverage.

     

    @istest

    public class TestAccountRelatedlistController {

    Static testmethod void TestAccountmethod1(){

    List<Account> acclist = new list<Account>();

    Account acc = new account();

    acc.Name = 'test';

    acc.phone = '8974990495';

    acc.Email__c = 'test@ee.com';

    acc.Description = 'test des';

    //acclist.add(acc);

    //insert acclist;

    insert acc;

    opportunity opp = new opportunity();

    opp.Name = 'test';

    opp.StageName='Qualification';

    opp.CloseDate = system.today()+5;

    opp.AccountId = acc.id;

    insert opp;

    List<AccountRelatedListController.DataTableWrapper> wrapperList = AccountRelatedListController.getAllOpps(acc.id);

    }

    }

    If this helps, Please mark it as best answer.

    Thanks!!
0/9000