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
Dee Dee AaronDee Dee Aaron 

Help to create a test class

Hi there,

I have an Apex class that works, but I need to create a test class for it. I'm not a developer and don't know how, so I sincerely appreciate your help. Thank you.

Apex Trigger:
trigger FeedCommentTest on FeedComment (after insert) 
{
    Id profileId = UserInfo.getProfileId();
    String profileName =[Select Id, Name from Profile where Id=:profileId].Name;
    Set<id> SalesEngineeringSet = new Set<Id>();
    List<SALES_ENGINEERING_REQUEST__c> serList = new List<SALES_ENGINEERING_REQUEST__c>();
    for(FeedComment f : Trigger.New)
    {
        if(profileName  == 'Net Planning Department')
        {
            SalesEngineeringSet.add(f.ParentId);
        }          
    }
    
    if(!SalesEngineeringSet.IsEmpty()){
        for(SALES_ENGINEERING_REQUEST__c ser : [SELECT ID,Status__c FROM SALES_ENGINEERING_REQUEST__c WHERE ID In: SalesEngineeringSet]){
            if(ser.Status__c != 'Approved' && ser.Status__c != 'Unable to Meet Request'){
                ser.Status__c = 'Approved';
                serList.add(ser);
            }
        }
    }
    
    if(!serList.IsEmpty())
        update serList;
}
Best Answer chosen by Dee Dee Aaron
Kritika Raj (26/96)Kritika Raj (26/96)
Hi Dee Dee , I have created a sample test class for your trigger . Please let me know if you have any questions regarding this . If not Please mark this as the Best answer so other can reference it.
@isTest
public class FeedCommentTriggerTest {
    
    @isTest
    public static void testFeedCommentTrigger() {
        
        //Create a User with Profile 'Net Planning Department'
        Id pid = [Select Id FROM Profile WHERE Name = 'Net Planning Department'].Id;
        
        User u = new User();
        u.ProfileId = pid;
        //here fill all the required fields for the User Record
        insert u;
        
        SALES_ENGINEERING_REQUEST__c s = new SALES_ENGINEERING_REQUEST__c();
        s.Status__c = //any Value othe than "Approved" and "Unable to Meet Request"
        //here fill all the required fields for the SALES_ENGINEERING_REQUEST__c Record
        insert s;
        
        //Since test class runs in the current user mode and in this Trigger lines will be covered only when the    //User has a Profile - 'Net Planning Department' hence we need trigger this event as the new User created with this //Profile
        System.runAs(u) {
            FeedComment f = new FeedComment();
            f.ParentId = s.Id;
            //here fill all the required fields for FeedCommentRecord
            
            Test.startTest();
            insert f;
            Test.stopTest();
        }
        
        
        SALES_ENGINEERING_REQUEST__c updatedRecord = [SELECT Id,Status__c  FROM SALES_ENGINEERING_REQUEST__c WHERE Id =: s.Id];
        System.assertequals('Approved',updatedRecord.Status__c);
    }
    
}

All Answers

Kritika Raj (26/96)Kritika Raj (26/96)
Hi Dee Dee , I have created a sample test class for your trigger . Please let me know if you have any questions regarding this . If not Please mark this as the Best answer so other can reference it.
@isTest
public class FeedCommentTriggerTest {
    
    @isTest
    public static void testFeedCommentTrigger() {
        
        //Create a User with Profile 'Net Planning Department'
        Id pid = [Select Id FROM Profile WHERE Name = 'Net Planning Department'].Id;
        
        User u = new User();
        u.ProfileId = pid;
        //here fill all the required fields for the User Record
        insert u;
        
        SALES_ENGINEERING_REQUEST__c s = new SALES_ENGINEERING_REQUEST__c();
        s.Status__c = //any Value othe than "Approved" and "Unable to Meet Request"
        //here fill all the required fields for the SALES_ENGINEERING_REQUEST__c Record
        insert s;
        
        //Since test class runs in the current user mode and in this Trigger lines will be covered only when the    //User has a Profile - 'Net Planning Department' hence we need trigger this event as the new User created with this //Profile
        System.runAs(u) {
            FeedComment f = new FeedComment();
            f.ParentId = s.Id;
            //here fill all the required fields for FeedCommentRecord
            
            Test.startTest();
            insert f;
            Test.stopTest();
        }
        
        
        SALES_ENGINEERING_REQUEST__c updatedRecord = [SELECT Id,Status__c  FROM SALES_ENGINEERING_REQUEST__c WHERE Id =: s.Id];
        System.assertequals('Approved',updatedRecord.Status__c);
    }
    
}
This was selected as the best answer
Dee Dee AaronDee Dee Aaron
Hi Kritika. Thank you for providing a test class. Can I just use this or do I need to modify it? If I need to insert specific values, please let me know. Thank you so much for your answer. I will mark as "best". Just wanted to make sure that I could use this-- or find out what modifications are needed.
Dee Dee AaronDee Dee Aaron
There's a section that says:
//here fill all the required fields for the SALES_ENGINEERING_REQUEST__c Record insert s;

I only have 2 required fields on this object

Name (Name) *Standard Text field.
Layer (Layer__c) *This is a picklist. The options are "Layer 2" or "Layer 3"

Can you please insert them? I'm not sure how they need to be formatted.

*Question: I have some validation rules on this object. Will that cause any issues? (E.g. IF this field is blank then this other field is required). Thank you for your help.
Kritika Raj (26/96)Kritika Raj (26/96)
Hi Dee Dee Aaron ,

Wherever I have wriiten "here fill all the required fields" , needs a modification there . For Ex : As you said that 

Name (Name) *Standard Text field.
Layer (Layer__c) *This is a picklist. The options are "Layer 2" or "Layer 3"

are the required fields of SALES_ENGINEERING_REQUEST__c , then before "insert s " line you need to populate these fields as follows:

s.Name = 'Test SER 1';
s.Layer__c = 'Layer 2';

Similiarly populate all the required fields for User and FeedComment Object here.

*Question: I have some validation rules on this object. Will that cause any issues? (E.g. IF this field is blank then this other field is required) - Yes the Validation Rule will apply in the test class as well . So , you need to insert them in such a way that it does not hit the Validation Rule.
Dee Dee AaronDee Dee Aaron
I added the test class. When trying to deploy my apex trigger and test class that you provided, I got the following error: Your organization's code coverage is 0%. You need at least 75% coverage to complete this deployment. Also, the following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.