1

I have the following CaseTeam Trigger and CaseTeam Trigger Test Class... it runs but it is only providing 63% code coverage... can you please show me how I can increase my code coverage for this Trigger? Thank you in advance... I look forward to adding to your reputation!

TRIGGER

//My Trigger is as follows  
trigger AffectedSalesForceUserCaseTeamUpDate on Case (before update) 
{
    List<CaseTeamMember> ctm = new List<CaseTeamMember>();
    List<CaseTeamMember> ctmdelete = new List<CaseTeamMember>();
    List<CaseTeamRole>ctrole = [SELECT Name, Id FROM CaseTeamRole];
    Map<String,String>caseteamr = new Map<String,String>{};
        for(CaseTeamRole ct:ctrole){
            caseteamr.put(ct.Name, ct.Id);}

        // For each case
    for(case c: trigger.new){
        // Get the id of the old case
        Case oldCase = Trigger.oldMap.get(c.Id);
        //Delete old case team member from case team
        if(
           oldCase.Affected_Salesforce_User__c == null ||
           c.Affected_Salesforce_User__c != oldCase.Affected_Salesforce_User__c){
            for(CaseTeamMember ctmrem : [SELECT Id FROM CaseTeamMember WHERE MemberID =: c.Affected_Salesforce_User__c OR MemberID =: oldCase.Affected_Salesforce_User__c]){
                ctmdelete.add(ctmrem);}
        }

        // If the old case team member doesn't equal the new case team member
        if(oldCase.Affected_Salesforce_User__c != c.Affected_Salesforce_User__c && c.Affected_Salesforce_User__c != null){
            // Create a list of the members in the case team
            // For each of the case team members in ctmlist
            CaseTeamMember ctmadd = new CaseTeamMember();
            ctmadd.ParentId = c.id;
            ctmadd.MemberId = c.Affected_Salesforce_User__c;
            ctmadd.TeamRoleId = caseteamr.get('Affected User');
            // Add the case team member to the list ctm
            ctm.add(ctmadd);
        }
    }
    delete ctmdelete;
    if(ctm.isEmpty() == false){
        insert ctm;}}

UNIT TEST

//My TestCode is as follows  
@isTest(seeAllData=true)
    public class AffectedSalesForceUserCaseTeamUpDateTest
{
    static testMethod void AffectedSalesForceUserCaseTeamUpDate()
 {     

CaseTeamMember[] newmembers = new CaseTeamMember[]{};
integer  i;
 Case c= new Case(Affected_Salesforce_User__c = '00550000004fTqi');
 insert c;
 case u1=[select id,ownerid from case where id=:c.id];
 id uid=u1.ownerid;

//newmembers.add(ctm);
 CaseTeamMember ctm= new CaseTeamMember();
 ctm.ParentId = c.id;
 ctm.MemberId = c.Affected_Salesforce_User__c;
 ctm.TeamRoleId = ctm.get('Affected User');
 insert ctm;
 update c;
 system.assertequals(ctm.Parentid,c.id);


//This asserts that prior to adding the CaseTeamMember, there are only the correct caseteammembers in the system
 list<CaseTeamMember> ctms = [Select Id, MemberId From CaseTeamMember];
 system.assertEquals(967, ctms.size());

//Shows that after your trigger fires, the correct team members are in the system
 ctms = [Select Id, MemberId From CaseTeamMember];
 system.assertEquals(967, ctms.size());


 }
}  
5
  • What lines are getting coverage?
    – EricSSH
    Feb 3 '16 at 22:10
  • Line 12, 13, 14, 15, 16, 17, 20, 22, 24, 25, 26, 32, 43, and 44 are ALL getting Coverage... 27, 28, 35, 36, 37, 38, 40, 45 are NOT getting Coverage Feb 3 '16 at 22:19
  • This testmethod looks scary -- you are asserting that 967 caseteamMembers exist because you are using seeAllData=true. This testmethod is likely to break very soon. You should be mocking case, User, and caseTeamMembers
    – cropredy
    Feb 3 '16 at 22:54
  • @crop1645, could you possibly please provide an example of how I could do this... I am still learning how to do this whole coding thing? Feb 3 '16 at 23:03
  • you are actually pretty close -- remove seeAllData=true, mock a User and then use that User's id as the value for affected_salesforce_user__c, then your asserts will be looking for the absence of the mocked CTM and the presence of the new CTM
    – cropredy
    Feb 3 '16 at 23:09
1

For those of yall who see this in the future... Nathan Pepper of the Los Angeles SalesForce Developer Group, @MayTheSForceBeWithYou, was able to assist me in creating the following... thank you N8!

@isTest
private class AffectedSalesforceUserCaseTeamTest {
@isTest
private static void testPopulateAffectedSalesforceUser() {
}
@isTest
private static void testClearAffectedSalesforceUser() {
}
@isTest
private static void testSwitchAffectedSalesforceUser() {
    Case c = new Case(Status = 'New', Type = 'Question', Affected_Salesforce_User__c = '00550000004PBGc', Application__c = 'Salesforce', Priority = 'P3');
    insert c;
    c.Affected_Salesforce_User__c = UserInfo.getUserId();
    update c;

    User otherUser = [SELECT Id FROM User WHERE IsActive = true AND Id != :UserInfo.getUserId() LIMIT 1][0];
    c.Affected_Salesforce_User__c = otherUser.Id;

    Test.startTest();

    update c;

    Test.stopTest();

    List<CaseTeamMember> oldUserCtms = [SELECT Id, MemberId, ParentId FROM CaseTeamMember WHERE MemberId = :UserInfo.getUserId() AND ParentId = :c.Id],
                        newUserCtms = [SELECT Id, MemberId, ParentId FROM CaseTeamMember WHERE MemberId = :otherUser.Id AND ParentId = :c.Id];

    System.assertEquals(0, oldUserCtms.size(), 'Old user CaseTeamMembers should be deleted.');
    System.assertEquals(1, newUserCtms.size(), 'New user should have 1 CaseTeamMember.');
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.