function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TeddyAbleTeddyAble 

AGHH!! How come my Testcase is limiting to 31%?

Hello Guys

 

I have exhausted all avenue in trying to increase my Test coverage from 31% to atleast 75% but for some reason i dont know what else to do to allow the test case to reflect this.

 

Please guys If you can help me and solve this problem as it is killing me softly

 

 

public class A1 {

   public A1(ApexPages.StandardController Controller)
    
        {
            a=new adviser__c();
            b=new contact();
           // c=new account();

        }
     
    
            Public Adviser__c a {get;set;}
            public contact b {get;set;}
           // public account c {get;set;}


        public pageReference Save()

            {
            insert b;
            a.Contact__c=b.id;
            a.Name=b.FirstName+' '+b.LastName;

            insert a;
            b.Adviser__c=a.id;

            update b;
            pageReference pref=New Pagereference(URL.getSalesforceBaseUrl().toExternalForm()+'/apex/AdviserView?id='+a.id);


            return pref;
            }

      public pageReference Cancel()
        {
        return page.AdviserView;
        }


            static testmethod void CodeCoverageTests()
            {
                
            PageReference current_page = Page.AdviserView;
            Test.setCurrentPage(current_page);
                
                
                
            Adviser__c AB = new Adviser__c();
            ApexPages.StandardController sc = new ApexPages.StandardController(AB);
           // A1 ctrl = new A1(sc);
                
                               
            contact cc = new contact(adviser__c ='a0JQ0000002a7g1', FirstName='Charlie', LastName='Harper');
            insert cc;
                
                ApexPages.currentPage().getParameters().put('id',cc.id);
                
                ApexPages.StandardController ctrl = new Apexpages.Standardcontroller(cc);
               
            Adviser__c ac= new Adviser__c(name ='Charlie'+' '+'Harper', contact__c = '003Q000000U836P');
            insert ac;                
                
                ApexPages.currentPage().getParameters().put('id',ac.id);
                ApexPages.StandardController trl = new Apexpages.Standardcontroller(ac);
                                     
           //update cc;
          //  pageReference pref=New Pagereference(URL.getSalesforceBaseUrl().toExternalForm()+'/apex/AdviserView?id='+cc.id);
                
 
            
            ctrl.Save();
                trl.Save();
                  
         
            }

}

 

 

 

Thank you for your help

 

Regards,

bob_buzzardbob_buzzard

Can you tell us which lines aren't getting covered?  Also, do you see any errors when executing the test?

Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference:

 

public class A1 {

            Public Adviser__c a {get;set;}

            public contact b {get;set;}

           // public account c {get;set;}

    

   public A1(ApexPages.StandardController Controller)

   

        {

            a=new adviser__c();

            b=new contact();

           // c=new account();

 

        }

   

 

 

        public pageReference Save()

 

            {

            insert b;

            a.Contact__c=b.id;

            a.Name=b.FirstName+' '+b.LastName;

 

            insert a;

            b.Adviser__c=a.id;

 

            update b;

            pageReference pref=New Pagereference(URL.getSalesforceBaseUrl().toExternalForm()+'/apex/AdviserView?id='+a.id);

 

 

            return pref;

            }

 

      public pageReference Cancel()

        {

        return page.AdviserView;

        }

 

 

            static testmethod void CodeCoverageTests()

            {

               A1 obj = new A1();

     

      contact con = new contact();

      con.firstname = 'firstname';

      con.lastname = 'lastname';

      insert con;

     

      Adviser__c ad = new Adviser__c();

      ad.Contact__c = con.id;

               ad.name = con.FirstName+' '+con.LastName;

      insert ad;

     

      contact conupdate= [select id,name from contact where id=:con.id];

      conupdate.Adviser__c = ad.id;

      update conupdate;

                  obj.Save();

      obj.Cancel();

        

            }

 

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

SRKSRK

Try this

& never use existin record Id in Test methods

 

static testmethod void CodeCoverageTests()
    {
            static testMethod void quotepdfTestCases2()
            {
            PageReference current_page = Page.AdviserView;
            Test.setCurrentPage(current_page);
            Adviser__c AB = new Adviser__c();
            ApexPages.StandardController sc = new ApexPages.StandardController(AB);
           // A1 ctrl = new A1(sc);
           
           contact con = new contact();
                  con.firstname = 'firstname';
                con.lastname = 'lastname';
                insert con;
            
              Adviser__c ad = new Adviser__c();
              ad.Contact__c = con.id;
              ad.name = con.FirstName+' '+con.LastName;
              insert ad;
              
              con.adviser__c = ad.id;
              update con;
                        
            ApexPages.currentPage().getParameters().put('id',con.id);
            ApexPages.StandardController ctrl = new Apexpages.Standardcontroller(con);
           
                   
           //update cc;
          //  pageReference pref=New Pagereference(URL.getSalesforceBaseUrl().toExternalForm()+'/apex/AdviserView?id='+cc.id);
                
 
            
            ctrl.Save();
                
                  
         
            }
            
            static testMethod void quotepdfTestCases2()
            {
               contact con = new contact();
                  con.firstname = 'firstname';
                con.lastname = 'lastname';
                insert con;
            
              Adviser__c ad = new Adviser__c();
              ad.Contact__c = con.id;
              ad.name = con.FirstName+' '+con.LastName;
              insert ad;
              
              con.adviser__c = ad.id;
              update con;
            
            
                PageReference current_page = Page.AdviserView;
                Test.setCurrentPage(current_page);
                Adviser__c AB = new Adviser__c();
                ApexPages.StandardController sc = new ApexPages.StandardController(AB);
                ApexPages.currentPage().getParameters().put('id',ad.id);
                ApexPages.StandardController trl = new Apexpages.Standardcontroller(ad);
                trl.Save();
            }
    }

TeddyAbleTeddyAble

Hello AnKit

 

It has an error that says  Constructor Not Defined [A1].<Constructor>()

 

I think we need the constuctor to be defined,

Please get back to me when u can.

 

thanks in advance

TeddyAbleTeddyAble

Hello Bob,

 

Line 18 downwards is not covered.

 

And it ran without any execution Error.

TeddyAbleTeddyAble

Hello AnKit

 

With your suggested code it is stilll limiting me to 31%