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
sashamsasham 

Data is not inserting in the test class. any help on this (Test class)

I craeted custom button on  participant table and link Vf pageto the button . when i clicked on the button , address table will get updated 
This is my controller extension  and Vf page .i need help on the test calss 
public with sharing class PClass{

    public participant__c participant;
    private ApexPages.StandardController stdControler;
    public Id Id { get; set; }
    
    
    public PClass(ApexPages.StandardController stdControler){
        
        this.participant = (participant__c)stdControler.getRecord();
        this.stdControler = stdControler;
            
    }
    
    public PageReference InsertAddress(){
        	
        try{
            participant__c p = [select id, SS_ID__c 
                                from participant__c
                               where Id = :participant.Id limit 1];
                             
           Registration__c r= [select Id,Address1__c, Address2__c,SS_ID__c 
                                                                        from Registration__c 
                                                                        where SS_ID__c =:p.SS_ID__c limit 1];
                                                                        
            Addresses_c  paddress = new Addresses__c();
            paddress.participant__c = participant.Id;
            paddress.Address1__c = r.Address1__c;
            paddress.Address2__c = r.Address2__c;
            insert paddress;
	   }

	   catch (Exception e){}
           return new ApexPages.StandardController(participant).view();
}
VF page 
<apex:page standardController="Participant__c" extensions="PClass" action="{!InsertAddress}" >


</apex:page>
My test class
@isTest public  with sharing class PClass_Test {

    static TestMethod void AddAddress(){
        
        
        Registration__c  TestRegistration= new Registration__c();
        TestRegistration.Name = 'Test Registration';
        TestRegistration.SS_ID__c = Decimal.valueOf(30111);
        TestRegistration.Address1__c='10 Street';
        TestRegistration.Address2__c ='15 street';
        insert TestRegistration;
        
        participant__c TestParticipant = new participant__c();
        TestParticipant.FirstName__c ='Test participant';
        TestParticipant.SS_ID__c =Decimal.valueOf(30111);;
        insert TestParticipant;
        
	Test.startTest();
        ApexPages.StandardController stdController = new ApexPages.StandardController(TestParticipant);
        PClass  pa = new PClass(stdController);
       System.debug(pa.Id);
      
        
       	pa.InsertAddress();
        System.debug(pa);
        
        Test.stopTest();
        try{
        
     		Addresses_c padd = [select id, participant__c from Addresses_c where participant__c =:pa.Id limit 1];
           }
      catch (Exception e){}
}


 
Best Answer chosen by sasham
Akhilesh Reddy BaddigamAkhilesh Reddy Baddigam
Hi Su Su, please go through the following example which helps you achieve 100% code coverage for the given test class, i tried the same code to update contacts phone field based on account phone field when a button on contact page layout was clicked!

All you need to do for the test class is to check both for try block as well as catch block which can be achieved by inserting the corresponding object with not necessary data and try to run both the methods for testing!

 
public with sharing class PClass{

    public contact participant;
    
    public Id Id { get; set; }
    
    
    public PClass( ApexPages.StandardController stdControler ){
        
        this.participant = (contact)stdControler.getRecord();
        
            
    }
    
    public PageReference InsertAddress(){
        	
        try{
            contact p = [select id,AccountId, Phone 
                                from contact
                               where Id = :participant.Id limit 1];
            System.debug('contact is'+p);
                             
           Account r= [select Id,Phone 
                                                                        from Account
                                                                        where Id =:p.AccountId limit 1];
            System.debug('Account is'+r);
                                                                        
            Contact  contl = new contact(id=p.Id);
            contl.Phone = r.Phone;
            
            update contl;
            System.debug('Updated Contact');
	   }

	   catch (Exception e){}
           return new ApexPages.StandardController(participant).view();
}
}
Visual Force Page:
<apex:page standardController="Contact" extensions="PClass" action="{!InsertAddress}" >


</apex:page>
Test Class:
@isTest public  with sharing class PClass_Test {

    static TestMethod void tryBlock(){
        
        
       
        Account a=new account();
        a.Name='test';
        a.phone='7327624257';
        insert a;
        
        Contact c=new contact();
        c.Phone='7327838236';
        c.LastName='akhiltest';
        c.AccountId=a.id;
        insert c;
        
	Test.startTest();
        ApexPages.StandardController stdController = new ApexPages.StandardController(c);
        PClass  pa = new PClass(stdController);
       System.debug(pa.Id);
      
        
       	pa.InsertAddress();
        System.debug(pa);
       
        
        Test.stopTest();
        contact co=[select id,Phone from contact where id=:c.Id];
         System.assertEquals(co.Phone, a.Phone);
        
     		
          
}
    @isTest
    public static void catchBlock(){
        Contact ic=new contact();
        ic.Phone='7327838236';
        ic.LastName='akhiltest';
        insert ic;
        Test.startTest();
        ApexPages.StandardController stdController = new ApexPages.StandardController(ic);
        PClass  pa = new PClass(stdController);
        pa.InsertAddress();
        Test.stopTest();
        
    }
}


If this helps you to resolve your problem, please choose this as the best answer!

Thank you!

All Answers

Akhilesh Reddy BaddigamAkhilesh Reddy Baddigam
Hi Su Su, please go through the following example which helps you achieve 100% code coverage for the given test class, i tried the same code to update contacts phone field based on account phone field when a button on contact page layout was clicked!

All you need to do for the test class is to check both for try block as well as catch block which can be achieved by inserting the corresponding object with not necessary data and try to run both the methods for testing!

 
public with sharing class PClass{

    public contact participant;
    
    public Id Id { get; set; }
    
    
    public PClass( ApexPages.StandardController stdControler ){
        
        this.participant = (contact)stdControler.getRecord();
        
            
    }
    
    public PageReference InsertAddress(){
        	
        try{
            contact p = [select id,AccountId, Phone 
                                from contact
                               where Id = :participant.Id limit 1];
            System.debug('contact is'+p);
                             
           Account r= [select Id,Phone 
                                                                        from Account
                                                                        where Id =:p.AccountId limit 1];
            System.debug('Account is'+r);
                                                                        
            Contact  contl = new contact(id=p.Id);
            contl.Phone = r.Phone;
            
            update contl;
            System.debug('Updated Contact');
	   }

	   catch (Exception e){}
           return new ApexPages.StandardController(participant).view();
}
}
Visual Force Page:
<apex:page standardController="Contact" extensions="PClass" action="{!InsertAddress}" >


</apex:page>
Test Class:
@isTest public  with sharing class PClass_Test {

    static TestMethod void tryBlock(){
        
        
       
        Account a=new account();
        a.Name='test';
        a.phone='7327624257';
        insert a;
        
        Contact c=new contact();
        c.Phone='7327838236';
        c.LastName='akhiltest';
        c.AccountId=a.id;
        insert c;
        
	Test.startTest();
        ApexPages.StandardController stdController = new ApexPages.StandardController(c);
        PClass  pa = new PClass(stdController);
       System.debug(pa.Id);
      
        
       	pa.InsertAddress();
        System.debug(pa);
       
        
        Test.stopTest();
        contact co=[select id,Phone from contact where id=:c.Id];
         System.assertEquals(co.Phone, a.Phone);
        
     		
          
}
    @isTest
    public static void catchBlock(){
        Contact ic=new contact();
        ic.Phone='7327838236';
        ic.LastName='akhiltest';
        insert ic;
        Test.startTest();
        ApexPages.StandardController stdController = new ApexPages.StandardController(ic);
        PClass  pa = new PClass(stdController);
        pa.InsertAddress();
        Test.stopTest();
        
    }
}


If this helps you to resolve your problem, please choose this as the best answer!

Thank you!
This was selected as the best answer
sashamsasham
Thanks Akhilesh Reddy Baddigam.