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
Varun99Varun99 

Test class concern

Hi,

 

Am new to sales force test class writing test class for displaying records when contacts have any

cases showing error message how to cover these method in test class

==My method===

 

public void delete()
{
conrecords=true;
viewpanel=false;
editpanel=false;

id aid=apexpages.currentpage().getparameters().get('conname');
con=[select id,lastname, Accountid, name,phone,email,(select id, CaseNumber from cases) from contact where id=:aid];
if(con.cases.size()>0 )
{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Fatal, 'You donot delete this record. It related to cases or customerportal contact');
ApexPages.addMessage(myMsg);
}

}

 

Bold font code covered but how to cover if condition Any one can solve my issue?

 

 

Thanks in Advance.

MoUsmanMoUsman

Hi Varun99,

 

Please try this test methosd which is given below I have written for you you not tested but I beileve it will run perfectly.And donot forget to create an instance of your class.

private static testmethod void unit_testDelete() {
	Test.startTest();	
	Account objAcc = [SELECT Id, Name FROM Account limit 1];
	apexpages.currentpage().getparameters().put('conname',ObjAcc.id);
	//call your method here if yor class name is ABC then
	ABC test = new ABC();
	test.delete();
	Test.stopTest();
 }

 If it works for you please mar as solved and give kudos to help other !

 

--

Thanks

Usman

SRKSRK
Grate Efforts MoUsman , nice

There few thing you needto take care
first of all try to create all the data with in the test method do not query ant data from the that not a good practice

so Varun99 in place of query a account create a new account with in your test method

& if you test method is in 27th version it might cause some issue so you can use (seealldata = true)
or can decrees the version to 25 or low
MoUsmanMoUsman

Thanks SRK for your suggestion and I agreed with you but we should create Mock record instead of (seealldata = true) to run the Test method this was a temporary solution! And Thank you so much for your suggestion.

 

--

Usman

Ayub HeartForceAyub HeartForce

hi varun

public static testmethod void test(){
Contact con = new Contact(
                      //put required field with value
                      ) public static testmethod void test(){
Contact con = new Contact(
                      //put required field with custom value
                      )
insert con;
apexpages.currentpage().getparameters().put('conname', con);
//initialize class obj
obj.yourmethod();

 

}