+ Start a Discussion
StaciStaci 

test class for commentbody

I have the following trigger and class, when an option is picked from a Case picklist called Macro, the corresponding case comment is to be added to the case.  I have tried various options I have found googling "test class for commentbody' but none seem to be covering the code in the last box.  The code works fine when updating a case, just needing the code coverage. 
trigger CW_DSS_CaseTriggerTSC50 on Case (after update) {
if(trigger.isafter && trigger.isupdate)
{
   CW_DSS_CaseTriggerHelper.CaseMethod1(Trigger.New,Trigger.oldMap);
}
  }
public class CW_DSS_CaseTriggerHelper{
           
           public static void CaseMethod1(List<Case> caseList, Map<Id,Case> oldmapValue){
           
if(CWcheckRecursive.runOnce())
    {
  
   List<caseComment> caseCommentList = new List<caseComment>();
    for (Case c: caseList)
    {
        
        caseComment cc = new caseComment();
        if(c.Macros__c == 'Application Support information'){
                cc.parentid = c.ID;
                cc.commentbody = 'For application (DSSi and Relay Server) support and troubleshooting requests, please provide the following information.'+'\n' +
                                    'Minimum DSS Ticket Information requirements:'+'\n' +
                                    'Site Name:' +'\n' +
                                    'Site Contact: (For possible Site IT related issues)' +'\n' +
                                    'DSSi Software Version:' +'\n' +
                                    'Problem/Inquiry:' +'\n' +
                                    'Troubleshooting Steps Taken:' +'\n' +
                                    'This information is required with all DSS support requests. Failure to provide this requested information may delay your request.';
                cc.ispublished = true;
                casecommentlist.add(cc);
                System.debug('********************************************************************'+ cc.commentbody);
               
        }
//insert caseCommentList;
            If(caseCommentList.Size() > 0){
            insert caseCommentList;
}}
           
           }
           
           
           }
}
/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class TestCW_DSS_CaseTrigger{
    
    
    
    static testMethod void CW_DSS_CaseTriggerTest(){
        Id caseRecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Detect - Fatigue Management (DSS)').getRecordTypeId();
        ID accRecordTypeID = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Worksite').getRecordTypeId();
        Account a1 = new Account(Name='Carlin Test', RecordTypeID = accRecordTypeID, DSS_Account__c = true);
        insert a1;
      
        Case cs1 = new Case();
cs1.RecordTypeId = caseRecordTypeId;
cs1.Site_Dealer_Name_DSS__c = a1.Id;
cs1.OwnerId = UserInfo.getUserId();
cs1.CW_Type__c = 'Question';
cs1.Status = 'New';
cs1.Subject = 'This is my case';
cs1.Description = 'This is my description';
cs1.Incident_Start__c=Date.today();
cs1.Priority = 'Normal';
cs1.Subsystem_Type__c = 'Detect Analytics - API';
insert cs1;
    }

    static testMethod void testCaseMacros1() {
   
    
    Integer count = Limits.getLimitQueries() + 1;
    // set up case
    Id ortId = [SELECT Id, Name FROM RecordType WHERE Name='Detect - Fatigue Management (DSS)'].id;
    RecordType rtAcct = [select Id from RecordType WHERE Name = 'Worksite' and SobjectType = 'Account' limit 1];
    //create account
    Account a1 = new Account(Name='Carlin Test', RecordType = rtAcct, DSS_Account__c = true);
    User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];        
    System.runAs (thisUser){ 
        
        Case cse = new Case(RecordTypeId = ortId, Site_Dealer_Name_DSS__c = a1.Id, CW_Type__c = 'Incident', Subsystem_Type__c = 'Detect Analytics - API',
        Status = 'New', Subject = 'This is my case', Description = 'This is my description', Incident_Start__c=Date.today());
        Database.insert(cse);
        
        //cse.Macros__c = 'Application Support information';
       // Database.update(cse);
        
       test.startTest();
    cse.Macros__c = 'Application Support information';
    update cse;
    
    Case cs = [Select Id,Macros__c from case where Id=:cse.Id limit 1];
    system.debug('cs --> ' + cs);
    system.debug('cs --> ' + cs.Macros__c);
    system.assertEquals('Application Support information' , cs.Macros__c);
    
    List<caseComment> caseCommentList = [Select Id from caseComment];
    system.assert(caseCommentList.size()>0, 'caseComment not created');
    test.stopTest();
      
        
    
        
      }}}
 
cc.parentid = c.ID;
                cc.commentbody = 'For application (DSSi and Relay Server) support and troubleshooting requests, please provide the following information.'+'\n' +
                                    'Minimum DSS Ticket Information requirements:'+'\n' +
                                    'Site Name:' +'\n' +
                                    'Site Contact: (For possible Site IT related issues)' +'\n' +
                                    'DSSi Software Version:' +'\n' +
                                    'Problem/Inquiry:' +'\n' +
                                    'Troubleshooting Steps Taken:' +'\n' +
                                    'This information is required with all DSS support requests. Failure to provide this requested information may delay your request.';
                cc.ispublished = true;
                casecommentlist.add(cc);


 
SwethaSwetha (Salesforce Developers) 
Hi Staci,
Can you highlight the lines of code that are not being covered?Thanks
StaciStaci
-->cc.parentid = c.ID;
               --> cc.commentbody = 'For application (DSSi and Relay Server) support and troubleshooting requests, please provide the following information.'+'\n' +
                                    'Minimum DSS Ticket Information requirements:'+'\n' +
                                    'Site Name:' +'\n' +
                                    'Site Contact: (For possible Site IT related issues)' +'\n' +
                                    'DSSi Software Version:' +'\n' +
                                    'Problem/Inquiry:' +'\n' +
                                    'Troubleshooting Steps Taken:' +'\n' +
                                    'This information is required with all DSS support requests. Failure to provide this requested information may delay your request.';
                -->cc.ispublished = true;
               --> casecommentlist.add(cc);
@Swetha I put --> on the lines