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
Caroline Poole 9Caroline Poole 9 

Apex Trigger - No Test Class

Hi. I found this apex class sample online. It triggered successfully in my sandbox. The trigger finds a string in a custom field and replaces it with a new character. When deploying to my production org, I realized I don't have a test class.

This is the Apex Trigger:
trigger replaceCharactersSB on rC_Connect__Batch_Upload__c (before insert) {
 String str = '>';
    String str2 = '>';
    String replacement = '>';
    for(rC_Connect__Batch_Upload__c des : trigger.new){
        if(des.SB_donation_allocations__c.contains(str)){
            des.SB_donation_allocations__c = des.SB_donation_allocations__c.replace(str, replacement);
        }
        if(des.SB_donation_allocations__c.contains(str2)){
            des.SB_donation_allocations__c = des.SB_donation_allocations__c.replace(str2, replacement);
        }
    }
}

Anyone know how to create a test class for this? Thank you for your help!
Best Answer chosen by Caroline Poole 9
Daniel AhlDaniel Ahl

Hm, try this then:

@isTest 
private class replaceCharactersSBTest {
    @isTest
    static void testReplaceCharactersSB(){
        List<rC_Connect__Batch_Upload__c> CBUs = new List<rC_Connect__Batch_Upload__c>();
        for(Integer i = 0; i < 100; i++){
            rC_Connect__Batch_Upload__c CBU = new rC_Connect__Batch_Upload__c(SB_donation_allocations__c = 'This is a test &amp;gt; string');
            CBUs.add(CBU);
        }
        for(Integer i = 0; i < 100; i++){
            rC_Connect__Batch_Upload__c CBU = new rC_Connect__Batch_Upload__c(SB_donation_allocations__c = 'This is a test &gt; string');
            CBUs.add(CBU);
        }
        
        Test.startTest();
        String expected = 'This is a test > string';
        insert CBUs;
        List<rC_Connect__Batch_Upload__c> insertedCBUs = [SELECT Id, SB_donation_allocations__c FROM rC_Connect__Batch_Upload__c];
        for(rC_Connect__Batch_Upload__c CBU : insertedCBUs){
            System.assertEquals(expected, CBU.SB_donation_allocations__c);
        }
        Test.stopTest();
    }
}
Does it say which row the "variable does not exist: CBU" error happens on?

All Answers

RituSharmaRituSharma
This trigger is for before insert scenario. In your test class, you will need to insert records of rC_Connect__Batch_Upload__c  and SB_donation_allocations__c should contain &amp;gt; and &gt;.
Caroline Poole 9Caroline Poole 9
@ritusharma - do you have any idea what that would look like? 
 
Daniel AhlDaniel Ahl
Assuming that SB_donation_allocations__c is a text field, it could look like this: 
@isTest 
private class replaceCharactersSBTest {
    @isTest
    static void testReplaceCharactersSB(){
        List<rC_Connect_Batch_Upload__c> CBUs = new List<rC_Connect_Batch_Upload__c>();
        for(Integer i = 0; i < 100; i++){
            rC_Connect_Batch_Upload__c CBU = new rC_Connect_Batch_Upload__c(SB_donation_allocations__c = 'This is a test &amp;gt; string');
            CBUs.add(CBU);
        }
        for(Integer i = 0; i < 100; i++){
            rC_Connect_Batch_Upload__c CBU = new rC_Connect_Batch_Upload__c(SB_donation_allocations__c = 'This is a test &gt; string');
            CBUs.add(CBU);
        }
        
        Test.startTest();
        String expected = 'This is a test > string';
        insert CBUs;
        List<rC_Connect_Batch_Upload__c> insertedCBUs = [SELECT Id, SB_donation_allocations__c FROM rC_Connect_Batch_Upload__c];
        for(rC_Connect_Batch_Upload__c CBU : insertedCBUs){
            System.assertEquals(expected, CBU.SB_donation_allocations__c);
        }
        Test.stopTest();
    }
}

 
Caroline Poole 9Caroline Poole 9
Hi Daniel- I am getting errors that say

variable does not exist: CBU
and
Invalid type: rC_Connect_Batch_Upload__c
Daniel AhlDaniel Ahl

Hm, try this then:

@isTest 
private class replaceCharactersSBTest {
    @isTest
    static void testReplaceCharactersSB(){
        List<rC_Connect__Batch_Upload__c> CBUs = new List<rC_Connect__Batch_Upload__c>();
        for(Integer i = 0; i < 100; i++){
            rC_Connect__Batch_Upload__c CBU = new rC_Connect__Batch_Upload__c(SB_donation_allocations__c = 'This is a test &amp;gt; string');
            CBUs.add(CBU);
        }
        for(Integer i = 0; i < 100; i++){
            rC_Connect__Batch_Upload__c CBU = new rC_Connect__Batch_Upload__c(SB_donation_allocations__c = 'This is a test &gt; string');
            CBUs.add(CBU);
        }
        
        Test.startTest();
        String expected = 'This is a test > string';
        insert CBUs;
        List<rC_Connect__Batch_Upload__c> insertedCBUs = [SELECT Id, SB_donation_allocations__c FROM rC_Connect__Batch_Upload__c];
        for(rC_Connect__Batch_Upload__c CBU : insertedCBUs){
            System.assertEquals(expected, CBU.SB_donation_allocations__c);
        }
        Test.stopTest();
    }
}
Does it say which row the "variable does not exist: CBU" error happens on?
This was selected as the best answer