• Doze2Katzs
  • NEWBIE
  • 25 Points
  • Member since 2011

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 6
    Replies

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

  • March 22, 2011
  • Like
  • 0

I am trying to create a bit of Apex code that will loop through accounts, find the associated contacts and then loop through the contacts to get their names. I'm not having much luck, but has anyone done this before and knows how to do it?

HI,

 

Please Help me in writing Test Class for this :

 

 

public class CancelCase {

    public String strCaseId = ApexPages.currentPage().getParameters().get('id');
    public String strClosingComments {get; set;}    
    public Case objCancelCase {get; set;}
    public List<CaseComment> objCancelCaseCommentList {get; set;}
    
    public CancelCase(ApexPages.StandardController controller) {
        strClosingComments ='';
        objCancelCaseCommentList = [Select Id, ParentId, IsPublished, CommentBody, CreatedById, CreatedDate, SystemModstamp,
                                    LastModifiedDate, LastModifiedById, IsDeleted, ConnectionReceivedId, ConnectionSentId
                                    From CaseComment where ParentId =:strCaseId ];
       }
    
    public List<SelectOption> getStatus(){
      List<SelectOption> options = new List<SelectOption>();
      Schema.DescribeFieldResult fieldResult = Case.Status.getDescribe();
      options.add(new SelectOption('Cancelled', 'Cancelled'));
      return options;
      }
    
    public PageReference save() {
   
      PageReference createCancelCase;
      try{
       if(strClosingComments==null || strClosingComments==''){
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.error,'You must enter Comments'));
              return null;
       }
    
        CaseComment  objLatestCancelComment = new CaseComment();
        objLatestCancelComment.ParentId = strCaseId;
        objLatestCancelComment.CommentBody= strClosingComments;
        objCancelCaseCommentList.add(objLatestCancelComment);
        upsert objCancelCaseCommentList ;
        
        //strCaseId
        Case caseBeingCancelled = [Select Status From Case where Id=:strCaseId];
        caseBeingCancelled.Status = 'Cancelled';
        update caseBeingCancelled;
        
        createCancelCase = new PageReference('/'+strCaseId);
        createCancelCase.setRedirect(TRUE);
 
        }catch(Exception e){
          }
      return createCancelCase ;

}

}

  • March 25, 2011
  • Like
  • 0

Hi,

 

I've an xml file. I have to read data from XML and display on visualforce page and have to update some objects based on that data.

 

Can any one of you please help on this...

 

 

Thanks

Anu

I have built a trigger on an object that updates fields on a related object.  This related object has many validation rules in place that are causing issues in building test data in my test class for this trigger.  My current approach is to create the needed test data in the correct manner to allow the validation rules to pass.  However, this means that if these rules change in the future, the test class might likewise also need to be updated.  In an effort to produce development that is low-maintainence in nature, flexible, and adaptable to changes, I would prefer to not use this approach.

 

Does anyone have any best practices or guidance on ways to write APEX tests that would remove the dependence on the validation rules?  The way I see it, the validation rules shouldn't cause my APEX tests to fail, as they would not indicate any defects in the APEX code itself, but the test data being generated.  Therefore, should we exclude these rules and leave the testing of them to System or Functional User testing?

 

The only solution I have come up with so far is to create a hidden "isTest" checkbox on the object with the validation rules.  This check box would be used in the rules to exclude records having this flag True.  This flag would only get set by my test class, and therefor circumvent the rules.  I'm sure there is some flaw in this approach that I am not thinking of, but am not sure.

 

 

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

  • March 22, 2011
  • Like
  • 0