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
glynnglynn 

a newbie testmethod unit test question

I have a testMethod and most of it works fine. However, there is a part of my Apex trigger that my test case is not getting into (executing).

 

 

 //Logic to handle different email subjects for each FeedItem type
        if (f.Type == 'ContentPost')
        {
            emailSubject = 'New ContentPost';
                     
            if (f.ContentData != NULL)
            {
                Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
                efa.setBody(f.ContentData);
                efa.setFileName(f.ContentFileName);
                mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
                tiBod=tiBod+'\n'+'***See Attached File!';
            }
            else
            {
                Map<String,Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();   
                List<Schema.SObjectType> sobjects = schemaMap.values();
                List<Sobject> theObjectResults;
                Schema.DescribeSObjectResult objDescribe;
                List<Schema.SObjectField> tempFields;
                for(Schema.SObjectType objType : sobjects)
                {
                    objDescribe = objType.getDescribe(); 
                    String sobjectPrefix = objDescribe.getKeyPrefix();
                    if(parentid != null && sobjectPrefix != null && parentid.startsWith(sobjectPrefix))
                    {
                        string objectType = objDescribe.getLocalName();
                        objectType = objectType + 'Feed';
                        
                        String qryString = 'SELECT contentData FROM ' + objectType + ' WHERE id' + '=' + '\'' + f.id + '\'';  
                    
                        sobject qr = Database.query(qryString);
                        Blob resultObject = (Blob)qr.get('ContentData');
                         
                        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
                        efa.setBody(resultObject);
                        efa.setFileName(f.ContentFileName);
                        mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
                    }
                }
            }        
        }
        else if  (f.Type == 'LinkPost')
        {
            emailSubject = 'New LinkPost';
            tiBod = tiBod + '\n' + 'Link: ' + f.linkUrl;
        }
        else
        {
            emailSubject = 'New TextPost';
        }

 

 

How do I test the code within the outside IF and ELSE IF statements. The code that starts at

 

 

(f.Type == 'ContentPost')

 

 

Once in the IF statement I'm also trying to figure out how to test the MAP and LIST types.

Appreciate any help you can give this newbie. thanks

Best Answer chosen by Admin (Salesforce Developers) 
Doze2KatzsDoze2Katzs

It would of been nice to see the test code, but from your other code it seems that your IF statement depends on the 'FeedItem' type. I guess this is a custom object.

So in your test code, you could either do two things:

 1. Create a 'FeedItem' and set its Type field to 'ContentPost' then update the record and change the type to 'LinkPost'. Finally, change the type to something besides 'ContentPost' and 'LinkPost'.
 2. Create three different 'FeedItem' records and set one Type's field to 'ContentPost'. The other to 'LinkPost'. Finally, change the last type to something besides 'ContentPost' and 'LinkPost'.

I usually do the first, because if I have any validation rules on the 'FieldItem' object then I can make sure the test code works on both insert and update operations.

An example of #2 follows:

 

static testMethod void testFieedItem() {
 LIST<feedItem> feeds = new LIST<feedItem>();
 FeedItem f1 = new FeedItem(type='ContentPost', ***ANY OTHER REQUIRED FIELDS THAT NEED TO BE SET ***);
 feeds.add(f1);
 FeedItem f2 = new FeedItem(type='LinktPost', ***ANY OTHER REQUIRED FIELDS THAT NEED TO BE SET ***);
 feeds.add(f2);
 FeedItem f3 = new FeedItem(type='Other', ***ANY OTHER REQUIRED FIELDS THAT NEED TO BE SET ***);
 feeds.add(f3);
 test.StartTest();
 insert feeds; //This will insert the three feeditems with 3 different types, which should check each of the branches of the if statement.
 test.StopTest();
 }

 

 

All Answers

Doze2KatzsDoze2Katzs

It would of been nice to see the test code, but from your other code it seems that your IF statement depends on the 'FeedItem' type. I guess this is a custom object.

So in your test code, you could either do two things:

 1. Create a 'FeedItem' and set its Type field to 'ContentPost' then update the record and change the type to 'LinkPost'. Finally, change the type to something besides 'ContentPost' and 'LinkPost'.
 2. Create three different 'FeedItem' records and set one Type's field to 'ContentPost'. The other to 'LinkPost'. Finally, change the last type to something besides 'ContentPost' and 'LinkPost'.

I usually do the first, because if I have any validation rules on the 'FieldItem' object then I can make sure the test code works on both insert and update operations.

An example of #2 follows:

 

static testMethod void testFieedItem() {
 LIST<feedItem> feeds = new LIST<feedItem>();
 FeedItem f1 = new FeedItem(type='ContentPost', ***ANY OTHER REQUIRED FIELDS THAT NEED TO BE SET ***);
 feeds.add(f1);
 FeedItem f2 = new FeedItem(type='LinktPost', ***ANY OTHER REQUIRED FIELDS THAT NEED TO BE SET ***);
 feeds.add(f2);
 FeedItem f3 = new FeedItem(type='Other', ***ANY OTHER REQUIRED FIELDS THAT NEED TO BE SET ***);
 feeds.add(f3);
 test.StartTest();
 insert feeds; //This will insert the three feeditems with 3 different types, which should check each of the branches of the if statement.
 test.StopTest();
 }

 

 

This was selected as the best answer
glynnglynn
thank you