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
Sunay - KVP Bus SolnsSunay - KVP Bus Solns 

Help on writing test method for a Apex Class

Hi,

 

I have a Apex class written as below. I need help on how to write test method for the same:

 

public with sharing class Controller{

  
    
    public ApexPages.StandardController standardContactController;
        
    public Controller(ApexPages.StandardController cntrl) {
        
        standardContactController = cntrl;
      
    }
    public pageReference doSaveAll() 
    {
        
        pageReference p;
         
        try
        {
         Guest_Profile__c gp = (Guest_Profile__c)standardContactController.getRecord();
         
         standardContactController.save();
         
         
         p=new pageReference ( 'https://idsfortune-developer-edition.na12.force.com/apex/My_Profile_Edit?id='+gp.id+'&save=0');
          
        }
        catch(Exception e)
        {
        
        }
        return p;
    }
    
    
     public pageReference doEdit() 
    {
        
        pageReference p;
         
        try
        {
         Guest_Profile__c gp = (Guest_Profile__c)standardContactController.getRecord();
         
         standardContactController.edit();
         
         
         p=new pageReference ( 'https://idsfortune-developer-edition.na12.force.com/apex/My_Profile_Save?id='+gp.id+'');
          
        }
        catch(Exception e)
        {
        
        }
        return p;
    }
}

 

 

cloudmaniacloudmania

private class TestClass

{

static testMethod void YourTestMethod()

       {

//first Create dummt Guest_Profile__c record

Guest_Profile__c    gp=new Guest_Profile__c(Name='Test,....);

              Apexpages.Standardcontroller GPController=new ApexPages.Standardcontroller(gp);

              Controller cntrl=new Controller(GPController);

              system.assertEqual(cntrl.doSaveAll(),'https://idsfortune-developer-edition.na12.force.com/apex/My_Profile_Edit?id='+gp.id+'&save=0);

   

            ...............//you can test other methods like above

          

}

}

Sunay - KVP Bus SolnsSunay - KVP Bus Solns

Hi,

 

I am getting this error when I try to compile the same:

 

 Compile Error: line breaks not allowed in string literals at line 9 column -1

 

 

Navatar_DbSupNavatar_DbSup

Hi,


Try the below code with test method:


public with sharing class Controller
{



public ApexPages.StandardController standardContactController;

public Controller(ApexPages.StandardController cntrl) {

standardContactController = cntrl;

}
public Controller()
{
}
public pageReference doSaveAll()
{

pageReference p;

try
{
Guest_Profile__c gp = (Guest_Profile__c)standardContactController.getRecord();

standardContactController.save();


p=new pageReference ( 'https://idsfortune-developer-edition.na12.force.com/apex/My_Profile_Edit?id='+gp.id+'&save=0');

}
catch(Exception e)
{

}
return p;
}


public pageReference doEdit()
{

pageReference p;

try
{
Guest_Profile__c gp = (Guest_Profile__c)standardContactController.getRecord();

standardContactController.edit();


p=new pageReference ( 'https://idsfortune-developer-edition.na12.force.com/apex/My_Profile_Save?id='+gp.id+'');

}
catch(Exception e)
{

}
return p;
}
static testMethod void Testing()
{
ApexPages.StandardController standardContactController;

Controller c1=new Controller();
Controller c=new Controller(standardContactController);
c.doEdit();
c.doSaveAll();

}
}

 

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

cloudmaniacloudmania

Did you copy and paste my code?Check the single quotes,there may be some missing syntax.....

cloudmaniacloudmania

system.assertEqual(cntrl.doSavelAll(),'https://idsfortune-developer-edition.na12.force.com/apex/My_Profile_Edit?id='+gp.id+'&save=0');

 

Check the syntax of this line...

Sunay - KVP Bus SolnsSunay - KVP Bus Solns

Hi,

 

I tried with the below code but it says Invalid Constructor name.

Sunay - KVP Bus SolnsSunay - KVP Bus Solns

Hi Erkan,

 

I checked the syntax but it says incorrect method or signature.

cloudmaniacloudmania

Post ur Whole test code please...

Sunay - KVP Bus SolnsSunay - KVP Bus Solns

@isTest
private class TestClass
{
public static testMethod void YourTestMethod()
{

Guest_Profile__c gp=new Guest_Profile__c(Guest_Name__c='Test');
Apexpages.Standardcontroller GPController=new ApexPages.Standardcontroller(gp);
Controller cntrl=new Controller(GPController);
system.assertEqual(cntrl.doSaveAll(),'https://idsfortune-developer-edition.na12.force.com/apex/My_Profile_Edit?id='+gp.id+'&save=0');



}
}

cloudmaniacloudmania

Double check ur controller name.

And Any other field is reuqired for GP_Profile__c object

Sunay - KVP Bus SolnsSunay - KVP Bus Solns

Hi,

 

I have checked that but still the below compile error:

 

Compile Error: Method does not exist or incorrect signature: system.assertEqual(System.PageReference, String) at line 10 column 15

Sunay - KVP Bus SolnsSunay - KVP Bus Solns

Hi,

 

I saved the test method successfully but the code coverage is showing only 15%. Please help on the same, also find the code below:

 

@isTest
private class TestClass
{
public static testMethod void YourTestMethod()
{
Guest_Profile__c gp=new Guest_Profile__c(Guest_Name__c='Test',Guest_Code__c= '1234');
Apexpages.Standardcontroller GPController=new ApexPages.Standardcontroller(gp);
Controller cntrl=new Controller(GPController);
//PageReference p= new PageReference('http://www.google.com');
//String abc = 'https://idsfortune-developer-edition.na12.force.com/apex/My_Profile_Edit?id='+gp.id+'&save=0';

system.assertEquals('cntrl.dosaveAll()','https://idsfortune-developer-edition.na12.force.com/apex/My_Profile_Edit?id='+gp.id+'&save=0');
}

{
Guest_Profile__c gp=new Guest_Profile__c(Guest_Name__c='Test',Guest_Code__c= '1234');
Apexpages.Standardcontroller GPController=new ApexPages.Standardcontroller(gp);
Controller cntrl=new Controller(GPController);
//PageReference p= new PageReference('http://www.google.com');
//String abc = 'https://idsfortune-developer-edition.na12.force.com/apex/My_Profile_Edit?id='+gp.id+'&save=0';

system.assertEquals('cntrl.doEdit()','https://idsfortune-developer-edition.na12.force.com/apex/My_Profile_save?id='+gp.id+'');
}
}

cloudmaniacloudmania

checked the uncovered lines by using SAlesforce test execution functionality,and try to cover by achieving related case.

Sunay - KVP Bus SolnsSunay - KVP Bus Solns

Hi,

 

Sorry I was not able to understand what you said in the earlier message. Can you please elaborate in detail?

WorkhardWorkhard

Hi,

Try the below code with test method:

public with sharing class Controller {

public ApexPages.StandardController standardContactController;

public Controller(ApexPages.StandardController cntrl)

{

standardContactController = cntrl;

}

public Controller() { }

public pageReference doSaveAll()

{

pageReference p;

try

{

Guest_Profile__c gp = (Guest_Profile__c)standardContactController.getRecord();

standardContactController.save();

p=new pageReference ( 'https://idsfortune-developer-edition.na12.force.com/apex/My_Profile_Edit?id='+gp.id+'&save=0');

}

catch(Exception e) { } return p; }

public pageReference doEdit()

{

pageReference p;

try {

Guest_Profile__c gp = (Guest_Profile__c)standardContactController.getRecord();

standardContactController.edit(); p=new pageReference ( 'https://idsfortune-developer-edition.na12.force.com/apex/My_Profile_Save?id='+gp.id+'');

}

catch(Exception e) { } return p; }

static testMethod void Testing()

{

ApexPages.StandardController standardContactController;

Controller c1=new Controller();

Controller c=new Controller(standardContactController);

c.doEdit();

c.doSaveAll();

}

}

Sunay - KVP Bus SolnsSunay - KVP Bus Solns

Hi workhard,

 

Thank you so much. The above method gave me 80% coverage. Is there a way to get 100% coverage for the same.

Sunay - KVP Bus SolnsSunay - KVP Bus Solns

Hi,

 

Can you help me on the test class for below Code:

 

trigger CalculateMaxPoints on Survey__c (before insert, before update)
{
   for(Survey__c srv:trigger.new)
      {  
         List<Survey_Question__c> sqrlist =[select Points__c,Type__c from   Survey_Question__c where Survey__c =:srv.id ];
         Integer maxpoint=0;
         for(Survey_Question__c sq:sqrlist)
         {     
             if((sq.Type__c!='Free Text')&&(sq.Points__c!=null))
             { 
               
               List<String>  pointList  = sq.Points__c.split('\n');
               if((sq.Type__c =='Single Select--Vertical')||(sq.Type__c =='Single Select--Horizontal')||(sq.Type__c =='Free Text'))
               {    
                  Integer pointsel=0;
                   for(Integer p = 0 ;p < pointList.size();p++ )
                   { 
                     
                     String point = pointList.get(p).trim();
                     if(pointsel < integer.valueof(point))
                     try
                     {
                     pointsel =  integer.valueof(point);
                     }
                     catch(Exception e)
                     {
                     
                     }
                     
                      
                   } 
                  maxpoint = maxpoint + pointsel;  
               }
                
               if(sq.Type__c =='Multi-Select--Vertical')             
                {  
                   String point;
                   for(Integer p = 0 ;p < pointList.size();p++ )
                   {  
                     try
                     {
                     maxpoint = maxpoint+ integer.valueof(pointList.get(p).trim());
                     }
                     catch(Exception e)
                     {
                     } 
                   } 
               } 
               
            }     
      }
      srv.Maximum_points__c = maxpoint;
       
   }
}

 

I am getting 27% coverage with the below test class, can anybody help me where I am going wrong:

 

 public static TestMethod void TestcalculateMaxpoint()
  {
      Survey__c srv = [Select id from Survey__c limit 1];
      Integer maxpoint=0;
      List<Survey_Question__c> sqrlist =[select Points__c,Type__c from   Survey_Question__c where Survey__c =:srv.id ];
        for(Survey_Question__c sq:sqrlist)
        {          
          if((sq.Type__c!='Free Text')&&(sq.Points__c!=null))
             { 
               
               List<String>  pointList  = sq.Points__c.split('\n');
               if((sq.Type__c =='Single Select--Vertical')||(sq.Type__c =='Single Select--Horizontal')||(sq.Type__c =='Free Text'))
               {    
                  Integer pointsel=0;
                   for(Integer p = 0 ;p < pointList.size();p++ )
                   { 
                     
                     String point = pointList.get(p).trim();
                     if(pointsel < integer.valueof(point))
                     try
                     {
                     pointsel =  integer.valueof(point);
                     }
                     catch(Exception e)
                     {
                     
                     }
                     
                      
                   } 
                  maxpoint = maxpoint + pointsel;  
               }
                
               if(sq.Type__c =='Multi-Select--Vertical')             
                {  
                   String point;
                   for(Integer p = 0 ;p < pointList.size();p++ )
                   {  
                     try
                     {
                     maxpoint = maxpoint+ integer.valueof(pointList.get(p).trim());
                     }
                     catch(Exception e)
                     {
                     } 
                   } 
               } 
               
            }     
      
      srv.Maximum_points__c = maxpoint;
   } 
  } 
 

Joe HickoxJoe Hickox

public class LeadSupportExtension {


    private ApexPages.StandardController controller;
    public LeadSupportExtension(ApexPages.StandardController controller) {
            this.controller = controller;
    }

    public PageReference saveAndFwd()
    {
        controller.save();
        PageReference successPage = Page.LeadCreateSuccess;
        successPage.setRedirect(true);
        return successPage;
    }

}

/* I am struglign with what needs to be tested in this code. This is part of a web to lead form and all the buttons and pages work fine in SB but need test coverage for this apex class to put into production. Please any help would be greatly appreciated as after years of being an admin I have recently became a developer and am eager to learn more.*/