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
Cristian TrifCristian Trif 

Coverage 100% for my class

So i have my test class which i wrote:

@isTest 
public class Controller2TestClass 
{
   static testMethod void testMethod1() 
   {
        Account acc = new Account();
        showAccount showAcc = new showAccount();
        acc.Name='Test';
        acc.Phone='0644204232';
        showAcc.save();
        
          PageReference pageRef = Page.screen2 ;
          Test.setCurrentPage(pageRef);
       
        String checkIdAccount = ApexPages.currentPage().getParameters().get('id');
       
        System.assertEquals(checkIdAccount, acc.id);
       
     }
}

VF Page:

<apex:page controller="showAccount" >
 <apex:form >
     <apex:pageBlock >
         <apex:pageBlockSection >
             <apex:outputField value="{!account.name}"/>
             <apex:inputField value="{!account.phone}"/>
             <apex:commandButton value="update" action="{!save}"/>
         </apex:pageBlockSection>
     </apex:pageBlock>
 </apex:form>
</apex:page>

And this is the class that I'm testing. I'm only covering 33% and I wonder how can I cover 100%? What I've missed? 
 
public class showAccount 
{
  public Account account{get;set;}
  
    public showAccount()
    {
        Id idAccount = apexpages.currentPage().getParameters().get('id'); 
        account=[Select ID, Name, Phone from Account where id = : idAccount];
    }
    
    public pageReference save(){
        update account;
        PageReference pageRef = new PageReference('/apex/screen3');
        pageRef.getParameters().put('id', account.Id);
        return pageRef;
    }
}

Best Answer chosen by Cristian Trif
Amit Chaudhary 8Amit Chaudhary 8
Please try to update your test class like below
@isTest 
public class Controller2TestClass 
{
   static testMethod void testMethod1() 
   {
        Account acc = new Account();
        acc.Name='Test';
        acc.Phone='0644204232';
		insert acc;
		
       
        ApexPages.currentPage().getParameters().put('id',acc.id);
		showAccount  showObj = new showAccount();  
		showObj.save();
        
		System.assertEquals(showObj.account != null);
       
     }
}

Let us know if this will help you
 

All Answers

arpit vijayvergiyaarpit vijayvergiya
Hello Cristian,
I made some changes in your test class. This will be helpful for you.
@isTest 
public class Controller2TestClass 
{
   static testMethod void testMethod1() 
   {
        Account acc = new Account();
        
        acc.Name='Test';
        acc.Phone='0644204232';
        showAcc.save();
        
          PageReference pageRef = Page.screen2 ;
          Test.setCurrentPage(pageRef);
       
        String checkIdAccount = ApexPages.currentPage().getParameters().get('id');
        
        System.assertEquals(checkIdAccount, acc.id);
        showAccount showAcc = new showAccount();
		showAcc.save();
     }
}
Mark as a solution if it helps you.
Thanks
 
Cristian TrifCristian Trif
I have an error. On line 10 you call the method save() and you didn't instantiate only at line 18. I'm kinda confused now.
Amit Chaudhary 8Amit Chaudhary 8
Please try to update your test class like below
@isTest 
public class Controller2TestClass 
{
   static testMethod void testMethod1() 
   {
        Account acc = new Account();
        acc.Name='Test';
        acc.Phone='0644204232';
		insert acc;
		
       
        ApexPages.currentPage().getParameters().put('id',acc.id);
		showAccount  showObj = new showAccount();  
		showObj.save();
        
		System.assertEquals(showObj.account != null);
       
     }
}

Let us know if this will help you
 
This was selected as the best answer
arpit vijayvergiyaarpit vijayvergiya
@isTest 
public class Controller2TestClass 
{
   static testMethod void testMethod1() 
   {
        Account acc = new Account();
        
        acc.Name='Test';
        acc.Phone='0644204232';
		insert acc;
        /*showAcc.save();
        PageReference pageRef = Page.screen2 ;
        Test.setCurrentPage(pageRef);
        */
        String checkIdAccount = ApexPages.currentPage().getParameters().put('id',acc.id);
        
        System.assertEquals(checkIdAccount, acc.id);
        showAccount showAcc = new showAccount();
		showAcc.save();
     }
}
Cristian TrifCristian Trif

It works!

Thank you Armit, damn you are good!