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
Linda 98Linda 98 

help with code coverage please

I am having a VF page and class and test class.I am getting 43% code coverage.How can i increae it?
Below is my aoex class which gets value from user on VF page and creates account.

When i debug the test classs,i am not getting account.name value even though i gave a.name='testaccount' in my test class.
So i am able to cover those lines of code.Please help me what am i missing.
 
public class new_classforpage {
    Public customobjectt__c pr{get;set;}
    
    Public string accountname{get;set;}
    public string contactname{get;set;}
    public string contactemail{get;set;}
   
    public Account account {get;set;}


    public new_classforpage(ApexPages.StandardController sc) {
    pr= (object__c )sc.getRecord();
    account=new account();
    }
    
    Public pagereference save(){
     upsert pr;

    if(accountname!= '' && accountname!= null){
      
        account.name=accountname;
       
        upsert account;
        
        contact c=new contact();
        c.accountid=account.id;
        c.lastName=contactname;
        c.Email=contactemail;;
        insert c;
            
        pr.account_Name__c=account.id;
        update Pr;
    }
    
   return new PageReference('/'+pr.Id+'/e?retURL=%2F'+pr.Id);   
    }
}

and this is my test class:
 
@istest
Public class test_classforpage{
    static testMethod void test(){
        test.startTest();        
        PageReference pref = Page.mypage;
        Test.setCurrentPage(pref);
        
        
        Account a =new account();

        a.name='accountname';
       
        if(a.name!= '' && a.name!= null){

        upsert a;
        
        contact c=new contact();
        c.lastname='test contact';
        c.accountid=a.id;
        c.email='testemail@gmail.com';
        insert c;
        
        Purchase_request__c pr =new Purchase_request__c();
        pr.account_Name__c=a.id;
        
        insert pr;
        
        update pr;

        // Instantiate standard Controller
        ApexPages.StandardController sc = new ApexPages.StandardController(pr);

        // Instantiate controller extension
        myclass  mc = new myclass (sc);
                
        mc.save();
           test.stopTest();
}
     }
}


 
Best Answer chosen by Linda 98
FearNoneFearNone
Linda 98,

but you didn't set a value to new_classforpage.accountname.
try adding this to your test class (or perhaps in the new_classforpage)
        myclass  mc = new myclass (sc);
        mc.accountname=a.name;
        mc.save();
        test.stopTest();
}

 

All Answers

FearNoneFearNone
Linda 98,

but you didn't set a value to new_classforpage.accountname.
try adding this to your test class (or perhaps in the new_classforpage)
        myclass  mc = new myclass (sc);
        mc.accountname=a.name;
        mc.save();
        test.stopTest();
}

 
This was selected as the best answer
Linda 98Linda 98
Thank you.It helped.But i had to set a value to contact lastname.Thank you.