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
sfdc007sfdc007 

Test class help

Hi,

I have a apex class for which i have test class for it , but its not covering any %, kindly help me on this regard

MY APEX CLASS :


public class IAR_TT_Excel_Export_Controller2 extends IarBaseController{ public string IARId {get;set;} public string GRIDId {get;set;} public IAR_Main__c objIARMain {get;set;} public string IARName {get;set;} public Decimal IARVersion {get;set;} public IAR_TT_Excel_Export_Controller2(){         IARId = ApexPages.currentPage().getParameters().get('IarMain');         GRIDId= ApexPages.currentPage().getParameters().get('id');         //EquipmentType = ApexPages.currentPage().getParameters().get('Type');           if(IARId!=null)             {                 objIARMain = [select Id, Customer_Name__c,IAR_Name__c,Version__c from IAR_Main__c where Id=:IARId LIMIT 1];                            }             IARName=objIARMain.IAR_Name__c;             system.debug('Before==>'+IARName);             IARName=IARName.replace(' ','_');             system.debug('After==>'+IARName);             IARVersion=objIARMain.Version__c;    }    }


MY TEST CLASS:

static testMethod void IAR_TT_Ramp_Controller_Test3() {
 
 //IAR_TT_Excel_Export_Controller2 iarexport  = new IAR_TT_Excel_Export_Controller2();
 
         // Add parameters to page URL
       // ApexPages.currentPage().getParameters().put('IarMain');
       
       
       IAR_Main__c iars1 = new IAR_Main__c();
       iars1.Customer_Requirements_Comments__c='check';
       iars1.Version__c=2;
       insert iars1;
      

 
 
 }

Kindly let me know what  am doing wrong here
thanks in advance
akhil Vakhil V
 IAR_Main__c iars1 = new IAR_Main__c();
       iars1.Customer_Requirements_Comments__c='check';
       iars1.Version__c=2;
       insert iars1;
ApexPages.currentPage().getParameters().put('IarMain',iaris1.id);
and pass all the values which are passing in constructor
IAR_TT_Excel_Export_Controller2 iarexport  = new IAR_TT_Excel_Export_Controller2();
Try it.

Thanks,
Akhil.
 
Adapala KullayappaAdapala Kullayappa
IAr_Main__c iars1=new IAr_Main__c ();
  iars1.Customer_Requirements_Comments__c='check';
       iars1.Version__c=2;
       insert iars1;
iars1.Customer_Requirements_Comments__c='check';
       iars1.Version__c=2;
       update iars1;
ApexPages.currentPage().getParameters().put('IarMain',iars1.id);
Mandatary Feilds And Those are inserted Apex class in feilds iff we takes in that feilds 
      IAR_TT_Excel_Export_Controller2 iarexport  = new IAR_TT_Excel_Export_Controller2();



Thanks 

Kulayappa
Amit Chaudhary 8Amit Chaudhary 8

Mail Class
public class IAR_TT_Excel_Export_Controller2 extends IarBaseController
{ 
	public string IARId {get;set;} 
	public string GRIDId {get;set;} 
	public IAR_Main__c objIARMain {get;set;} 
	public string IARName {get;set;} 
	public Decimal IARVersion {get;set;} 
	public IAR_TT_Excel_Export_Controller2()
	{         
		IARId = ApexPages.currentPage().getParameters().get('IarMain');         
		GRIDId= ApexPages.currentPage().getParameters().get('id');         
		//EquipmentType = ApexPages.currentPage().getParameters().get('Type');           
		if( IARId !=null )             
		{                 
			objIARMain = [select Id, Customer_Name__c,IAR_Name__c,Version__c from IAR_Main__c where Id=:IARId LIMIT 1];
		}             
		IARName=objIARMain.IAR_Name__c;             
		system.debug('Before==>'+IARName);             
		IARName=IARName.replace(' ','_');             
		system.debug('After==>'+IARName);             
		IARVersion=objIARMain.Version__c;    
	}   
}

Try below test class:- 
@isTest()
private class IAR_TT_Ramp_Controller_Test 
{
	static testMethod void IAR_TT_Ramp_Controller_Test3() 
	{
	   IAR_Main__c iars1 = new IAR_Main__c();
	   iars1.Customer_Requirements_Comments__c='check';
	   iars1.Version__c=2;
	   insert iars1;
	  
	   ApexPages.currentPage().getParameters().put('IarMain','IarMain');
	   ApexPages.currentPage().getParameters().put('id',iars1.id);
	   IAR_TT_Excel_Export_Controller2 obj = new IAR_TT_Excel_Export_Controller2();
	 }
}

NOTE: This code has not been tested and may contain typographical or logical errors

NOTE:- For Test classes we should consider below things :-
1) Try to avoid seealldata = true.
2) Always use system.assert in Test class.


Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help

Thanks
Amit Chaudhary
amit.salesforce21@gmail.com

 
sfdc007sfdc007
I tried the same, but i am getting the below error

System.QueryException: List has no rows for assignment to SObject


Stack TraceClass.IAR_TT_Excel_Export_Controller2.<init>: line 13, column 1
Class.IAR_Controller_Test.IAR_TT_Excel_Export_Controller2_Test3: line 63, column 1


its where i am refereing my controllerw
       IAR_TT_Excel_Export_Controller2 obj = new IAR_TT_Excel_Export_Controller2();

 
akhil Vakhil V
HI sfdc007,

@isTest
private class IAR_TT_Excel_Export_TC{
public static testmethod void IAR_TT_Excel(){
IAR_Main__c iars1 = new IAR_Main__c();
Customer_Name__c='Test3';
IAR_Name__c='Test2'
       iars1.Customer_Requirements_Comments__c='check';
       iars1.Version__c=2;
       insert iars1;

ApexPages.currentPage().getParameters().put('IarMain',iars1.id); 
ApexPages.currentPage().getParameters().put('id',?); ? Refers to the pass id what is this id specified in class?
IAR_TT_Excel_Export_Controller2 obj=new IAR_TT_Excel_Export_Controller2();

}
}
Amit Chaudhary 8Amit Chaudhary 8

Class:-
public class IAR_TT_Excel_Export_Controller2 extends IarBaseController
{ 
	public string IARId {get;set;} 
	public string GRIDId {get;set;} 
	public IAR_Main__c objIARMain {get;set;} 
	public string IARName {get;set;} 
	public Decimal IARVersion {get;set;} 
	public IAR_TT_Excel_Export_Controller2()
	{         
		IARId = ApexPages.currentPage().getParameters().get('IarMain');         
		GRIDId= ApexPages.currentPage().getParameters().get('id');         
		//EquipmentType = ApexPages.currentPage().getParameters().get('Type');           
		if( IARId !=null )             
		{                 
			objIARMain = [select Id, Customer_Name__c,IAR_Name__c,Version__c from IAR_Main__c where Id=:IARId  LIMIT 1];
		}             
		IARName=objIARMain.IAR_Name__c;             
		system.debug('Before==>'+IARName);             
		IARName=IARName.replace(' ','_');             
		system.debug('After==>'+IARName);             
		IARVersion=objIARMain.Version__c;    
	}   
}
Test Class:-
@isTest()
private class IAR_TT_Ramp_Controller_Test 
{
	static testMethod void IAR_TT_Ramp_Controller_Test3() 
	{
	   IAR_Main__c iars1 = new IAR_Main__c();
	   iars1.Customer_Requirements_Comments__c='check';
	   iars1.Version__c=2;
	   insert iars1;
	  
	   ApexPages.currentPage().getParameters().put('IarMain',iars1.id);
	   ApexPages.currentPage().getParameters().put('id',iars1.id);
	   IAR_TT_Excel_Export_Controller2 obj = new IAR_TT_Excel_Export_Controller2();
	 }
}

NOTE: This code has not been tested and may contain typographical or logical errors

NOTE:- For Test classes we should consider below things :-
1) Try to avoid seealldata = true.
2) Always use system.assert in Test class.


Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help

Thanks
Amit Chaudhary