+ Start a Discussion
Wylie PeachWylie Peach 

Simple Unit Test on Before Trigger that Concatenates Fields

Hi Everyone,

I wrote a simple trigger that concatenates some fields into a key__c, and now I am trying to test it. I am having trouble getting it to return the tested inputs. Also, I was thinking of doing some bulk testing as well, should I write a loop? Here is what I have so far: 
Trigger 
trigger Composite_Key on Purchases__c (before insert, before update) {
    if(Trigger.isBefore)
    {
        for(Purchases__c purchase : trigger.new)
        {
            String eventName = String.isBlank(purchase.Event_name__c)?'':purchase.Event_name__c+'-';
            String section = String.isBlank(purchase.section__c)?'':purchase.section__c+'-';
            String row = String.isBlank(purchase.row__c)?'':purchase.row__c+'-';
            String seat = String.isBlank(String.valueOf(purchase.seat__c))?'':String.valueOf(purchase.seat__c)+'-';
            String numseats = String.isBlank(String.valueOf(purchase.number_of_seats__c))?'':String.valueOf(purchase.number_of_seats__c)+'-';
            String adddatetime = String.isBlank(String.valueOf(purchase.add_datetime__c))?'':String.valueOf(purchase.add_datetime__c);
            purchase.Key__c = eventName + section + row + seat + numseats + adddatetime;
        }
    }
}

Test so far
@isTest
private class CompositeKeyTest {
    
    static testMethod void testPurchase() {
        //create a purchase to fire the trigger
        Purchases__c purchase = new Purchases__c(Event_name__c = 'test', section__c='test',row__c='test', seat__c=1,number_of_seats__c='test',add_datetime__c='test');
        
        insert purchase;
        System.Assert(purchase.Key__c=='testtesttest1testtest');
    }
}

 
Best Answer chosen by Wylie Peach
Maharajan CMaharajan C
Hi Wyile,

Try the beloe class:

@isTest
private class CompositeKeyTest {
    
    static testMethod void testPurchase() {
        //create a purchase to fire the trigger
        Purchases__c purchase = new Purchases__c(Event_name__c = 'test', section__c='test',row__c='test', seat__c=1,number_of_seats__c='test',add_datetime__c='test');
        
        insert purchase;
        System.assertEquals('testtesttest1testtest',purchase.Key__c,'Key is not Valid');
    }
    
    static testMethod void testbulkPurchase() {
        //create a purchase to fire the trigger
        List<Purchases__c> purchaseList = new List<Purchases__c>();
        for(Integer i=0 ; i < 10 ; i++ )
        {
            Purchases__c purchaserec = new Purchases__c(Event_name__c = 'test', section__c='test',row__c='test', seat__c= i+1 ,number_of_seats__c='test',add_datetime__c='test');
            purchaseList.add(purchaserec);
        }
        insert purchaseList;
        System.assertEquals('testtesttest5testtest',purchaseList[4].Key__c,'Key is not Valid');
    }


Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C

Could you please post the data type of all the fields you have refered in trigger.
Wylie PeachWylie Peach
They are all text fields except seat which is just a number. Is that what you mean?
Maharajan CMaharajan C
Hi Wyile,

Try the beloe class:

@isTest
private class CompositeKeyTest {
    
    static testMethod void testPurchase() {
        //create a purchase to fire the trigger
        Purchases__c purchase = new Purchases__c(Event_name__c = 'test', section__c='test',row__c='test', seat__c=1,number_of_seats__c='test',add_datetime__c='test');
        
        insert purchase;
        System.assertEquals('testtesttest1testtest',purchase.Key__c,'Key is not Valid');
    }
    
    static testMethod void testbulkPurchase() {
        //create a purchase to fire the trigger
        List<Purchases__c> purchaseList = new List<Purchases__c>();
        for(Integer i=0 ; i < 10 ; i++ )
        {
            Purchases__c purchaserec = new Purchases__c(Event_name__c = 'test', section__c='test',row__c='test', seat__c= i+1 ,number_of_seats__c='test',add_datetime__c='test');
            purchaseList.add(purchaserec);
        }
        insert purchaseList;
        System.assertEquals('testtesttest5testtest',purchaseList[4].Key__c,'Key is not Valid');
    }


Thanks,
Maharajan.C
This was selected as the best answer
Wylie PeachWylie Peach
Thank you, Maharajan! 

For clarity, why do you say the 'key is not valid' when it is written correctly in the first method? Also, in the second method, can you explain the logic behind seat__c = i+1, purchaseList[4].Key__c and how the output is 'testtesttest5testtest' ? Just trying to get my head around it. Thanks again :D
Wylie PeachWylie Peach
The code compiles, but the results failed 
System.AssertException: Assertion Failed: Key is not Valid: Expected: testtesttest1testtest, Actual: null
System.AssertException: Assertion Failed: Key is not Valid: Expected: testtesttest5testtest, Actual: null
I will keep tinkering. 
Maharajan CMaharajan C
Comment the assert lines and Run the test class then let me know is there any error is coming?

Tell me the code coverage
Wylie PeachWylie Peach
No errors now, 100% code coverage