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
Nagma KhanNagma Khan 

pelse any one support me what is the logic of the test class writing ?

hi

trigger PrvnentToDeleteContact on Contact (before Delete) {
    
    for(contact cont:trigger.old){
        if(cont.accountid==Null){
            cont.addError('you cant delete the contact because contact doest not connect the account');
        
        }
    
    }
}
how to write a test class ?
hi any one please give me a logic for the test class i have all knowlege but how i write please support ??

 
Harshit Garg 6Harshit Garg 6
Hi Nagma,

Since writing a test class to cover all of the facets of this class is not something that anyone on here will do for you, I can give you some pointers and hopefully get you started.  I would recommend that you do some reading on testing [1] [2] [3] [4] to get a better understanding.  Each of your individual tests should only tests one specific portion of you class (ie a constructor test, sendEmail test, contactSelected test, etc).  You should also have both a postitive (everything works perfectly) and a negative (things are not right) test.

Each test should follow the following structure:
Setup of test data. This includes creation of any data needed by your class.  Account, Contacts etc
Starting the test. This is calling Test.startTest() to reset the governor limits
Calling your class / method
Stopping the test.This is calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish
Asserting that your changes have worked
If you have inserted/updated/deleted data, you need to query for the updates
Run System.assert, System.assertEquals, System.assertNotEquals to verify that you got the correct data back
If you have any specific problems with your tests, feel free to create a new post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.

[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/
[4] http://blog.deadlypenguin.com/blog/testing/strategies/

Thanks,
Harshit garg
Vivian Charlie 1208Vivian Charlie 1208

Hi Nagma,

 

Unfortunately I do not believe this part of your statement please give me a logic for the test class i have all knowlege else you would have done it yourself and not posted here. Assuming you are a new developer with least knowledge of Salesforce I will help you this time and explain how it is done. However henceforth please do not ask someone to write your test class but write your own test class and then ask people for help where you maybe facing issues

 

@istest
private class contactDeleteTest {
private testmethod static void deleteContact(){
Contact objContact = new Contact();
objContact.Lastname = 'New Developer';
insert objContact; // set value to all Contact mandatory fields before inserting
try{
delete objContact;
}catch{exception ex}
// assert your error message here
}
}
 

Every test class is annotated with @isTest

Every test method is declared as testmethod static

Create dummy data as per your use case in the test method

For trigger perform respective DML to call the trigger

For VF Handlers call respective methods after setting values wherever applicable

 

Please check this standard help page and links related to it to understand test classes better

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_intro.htm

 

If this resolves your issue please mark this issue as resolved. All the best for your future tests and hope you do it yourself.

Thanks

Vivian