+ Start a Discussion
sfdc18sfdc18 

Test Class code coverage

Hi,

I am getting code coverage for first two lines(9%) of my trigger which checks the older and newer records.
Can you please help me increasing the code coverage to more than 75%.
Below is my code :
 
-----------------------------------------------------------------
Trigger
-----------------------------------------------------------------
trigger UserAvailabilityCheck on Case (before update) { 

if(stopRecursion.stopRecursiveTriggerRun ) {
    return;
}

stopRecursion.stopRecursiveTriggerRun = true;

Boolean oooFlag;
Set<ID> ids = Trigger.newMap.keyset();
ID oId;

List<Case> caseList = [Select Id, Status, OwnerId from Case where Id in : ids];

for(Case c : caseList) {
oId = c.OwnerId;
}

for (Case c : Trigger.New) {
    Case oldCase = Trigger.oldMap.get(c.Id);

    Boolean oldStatusClosed = oldCase.Status.equals('Closed');
    Boolean oldStatusACR = oldCase.Status.equals('Awaiting Customer Response');
    Boolean oldStatusResolved = oldCase.Status.equals('Resolved');
    Boolean newStatusInProgress = c.Status.equals('In Progress');

    if ((oldStatusClosed && newStatusInProgress)||(oldStatusACR && newStatusInProgress)||(oldStatusResolved && newStatusInProgress)) {

      Map<Id, boolean> onlineMap = n2de.DistributionEngineGlobal.getAvailabilityByUser(oId);

      for (Id id : onlineMap.keySet()) {  
      oooFlag = onlineMap.get(id);
      System.debug('===oooFlag ' + oooFlag);
      break;
      }
      if(!oooFlag){
      c.OwnerId = Label.Case_ReDistributeCasesId;
      }

    }
  }

}
-----------------------------------------------------------------
Test Class 
-----------------------------------------------------------------

@isTest
private class UserAvailabilityCheckTest {

    private  static  RecordType supportCaseRecordType  = [SELECT Name, Id FROM RecordType 
                                                          WHERE Name ='Support Case' 
                                                          AND SObjectType = 'Case'];
    static testMethod void testUserAvailabilityCheck() {
        
        User testUser = new User();
        testUser.Username= 'm98704nn@companyn.com';
        testUser.Email = 'testuser198704nn@companyn.com';
        testUser.Lastname = 'user4';
        testUser.Firstname = 'test4';
        testUser.Alias = 'test4';
        testUser.CommunityNickname = '123464';
        testUser.UserRole = [ select id from userrole where id ='00EG00000015wJk' ];
        testUser.ProfileId = '00eG0000000fz9y';
        
        //testUser.CurrencyIsoCode = 'USD';
        testUser.TimeZoneSidKey = 'GMT';
        testUser.LocaleSidKey = 'en_US';
        testUser.EmailEncodingKey = 'ISO-8859-1';
        testUser.LanguageLocaleKey = 'en_US';
        testUser.UserPermissionsMobileUser = false;

        insert testUser;
        
        User testUser2 = new User();
        testUser2.Username= 'm987041nn@companyn.com';
        testUser2.Email = 'testuser1987041nn@companyn.com';
        testUser2.Lastname = 'user41';
        testUser2.Firstname = 'test41';
        testUser2.Alias = 'test4';
        testUser2.CommunityNickname = '1234641';
        testUser2.UserRole = [ select id from userrole where id ='00EG00000015wJk' ];
        testUser2.ProfileId = '00eG0000000fz9y';
        
        //testUser.CurrencyIsoCode = 'USD';
        testUser2.TimeZoneSidKey = 'GMT';
        testUser2.LocaleSidKey = 'en_US';
        testUser2.EmailEncodingKey = 'ISO-8859-1';
        testUser2.LanguageLocaleKey = 'en_US';
        testUser2.UserPermissionsMobileUser = false;

        insert testUser2;

        Account   testAccount = new Account();
        testAccount.Name  = 'Test Account';
    
        insert testAccount;
    
        Contact   testContact = new Contact();
        testContact.LastName  = 'Test Name';
        
        Test.startTest();

        insert testContact;

        Product2  testProduct = new Product2();
        testProduct.Name                  = 'Test Product Name';
        testProduct.Product_Category__c   = 'Category';
        testProduct.Product_Family__c     = 'Family';
        testProduct.Product_Sub_family__c = 'Sub-Family';    

        insert testProduct;
    
        Case  testCase  = new Case();
        testCase.RecordTypeId = supportCaseRecordType.Id;
        testCase.Summary__c   = 'Summary';
        testCase.Description  = 'Description';
        testCase.Origin       = 'Email';
        testCase.Status       = 'Awaiting Customer Response';
        testCase.I_Agree__c   = true;
        testCase.ContactId    = testContact.Id;
        testCase.ProductId    = testProduct.Id;
        testCase.OwnerId      = testUser.Id;        

        insert testCase;

        testCase.OwnerId = testUser2.Id;  
        update testCase; 

        Test.stopTest();  

        Case c = [SELECT Status,OwnerID FROM Case WHERE Id = :testCase.Id];

        System.assertEquals( testUser2.Id, c.OwnerId );
            
    }
}
Best Answer chosen by sfdc18
AMIT KAMBOJAMIT KAMBOJ
Please use this updated code to boost coverage. Please mark this as solution if this resolves the issue.
@isTest
private class UserAvailabilityCheckTest {

    private  static  RecordType supportCaseRecordType  = [SELECT Name, Id FROM RecordType 
                                                          WHERE Name ='Support Case' 
                                                          AND SObjectType = 'Case'];
    static testMethod void testUserAvailabilityCheck() {
        
        User testUser = new User();
        testUser.Username= 'm98704nn@companyn.com';
        testUser.Email = 'testuser198704nn@companyn.com';
        testUser.Lastname = 'user4';
        testUser.Firstname = 'test4';
        testUser.Alias = 'test4';
        testUser.CommunityNickname = '123464';
        testUser.UserRole = [ select id from userrole where id ='00EG00000015wJk' ];
        testUser.ProfileId = '00eG0000000fz9y';
        
        //testUser.CurrencyIsoCode = 'USD';
        testUser.TimeZoneSidKey = 'GMT';
        testUser.LocaleSidKey = 'en_US';
        testUser.EmailEncodingKey = 'ISO-8859-1';
        testUser.LanguageLocaleKey = 'en_US';
        testUser.UserPermissionsMobileUser = false;

        insert testUser;
        
        User testUser2 = new User();
        testUser2.Username= 'm987041nn@companyn.com';
        testUser2.Email = 'testuser1987041nn@companyn.com';
        testUser2.Lastname = 'user41';
        testUser2.Firstname = 'test41';
        testUser2.Alias = 'test4';
        testUser2.CommunityNickname = '1234641';
        testUser2.UserRole = [ select id from userrole where id ='00EG00000015wJk' ];
        testUser2.ProfileId = '00eG0000000fz9y';
        
        //testUser.CurrencyIsoCode = 'USD';
        testUser2.TimeZoneSidKey = 'GMT';
        testUser2.LocaleSidKey = 'en_US';
        testUser2.EmailEncodingKey = 'ISO-8859-1';
        testUser2.LanguageLocaleKey = 'en_US';
        testUser2.UserPermissionsMobileUser = false;

        insert testUser2;

        Account   testAccount = new Account();
        testAccount.Name  = 'Test Account';
    
        insert testAccount;
    
        Contact   testContact = new Contact();
        testContact.LastName  = 'Test Name';
        
        Test.startTest();

        insert testContact;

        Product2  testProduct = new Product2();
        testProduct.Name                  = 'Test Product Name';
        testProduct.Product_Category__c   = 'Category';
        testProduct.Product_Family__c     = 'Family';
        testProduct.Product_Sub_family__c = 'Sub-Family';    

        insert testProduct;
    
        Case  testCase  = new Case();
        testCase.RecordTypeId = supportCaseRecordType.Id;
        testCase.Summary__c   = 'Summary';
        testCase.Description  = 'Description';
        testCase.Origin       = 'Email';
        testCase.Status       = 'Awaiting Customer Response';
        testCase.I_Agree__c   = true;
        testCase.ContactId    = testContact.Id;
        testCase.ProductId    = testProduct.Id;
        testCase.OwnerId      = testUser.Id;        

        insert testCase;
	stopRecursion.stopRecursiveTriggerRun = false; // Amit added
        testCase.OwnerId = testUser2.Id;
	testCase.Status = 'In Progress';	// Amit added for code coverage
        update testCase; 

        Test.stopTest();  

        Case c = [SELECT Status,OwnerID FROM Case WHERE Id = :testCase.Id];

        System.assertEquals( testUser2.Id, c.OwnerId );
            
    }
}

 

All Answers

AMIT KAMBOJAMIT KAMBOJ
Please try to use below updated test class and see if this resolves issue. If this resolves issue then please mark this answer as solution.
@isTest
private class UserAvailabilityCheckTest {

    private  static  RecordType supportCaseRecordType  = [SELECT Name, Id FROM RecordType 
                                                          WHERE Name ='Support Case' 
                                                          AND SObjectType = 'Case'];
    static testMethod void testUserAvailabilityCheck() {
        
        User testUser = new User();
        testUser.Username= 'm98704nn@companyn.com';
        testUser.Email = 'testuser198704nn@companyn.com';
        testUser.Lastname = 'user4';
        testUser.Firstname = 'test4';
        testUser.Alias = 'test4';
        testUser.CommunityNickname = '123464';
        testUser.UserRole = [ select id from userrole where id ='00EG00000015wJk' ];
        testUser.ProfileId = '00eG0000000fz9y';
        
        //testUser.CurrencyIsoCode = 'USD';
        testUser.TimeZoneSidKey = 'GMT';
        testUser.LocaleSidKey = 'en_US';
        testUser.EmailEncodingKey = 'ISO-8859-1';
        testUser.LanguageLocaleKey = 'en_US';
        testUser.UserPermissionsMobileUser = false;

        insert testUser;
        
        User testUser2 = new User();
        testUser2.Username= 'm987041nn@companyn.com';
        testUser2.Email = 'testuser1987041nn@companyn.com';
        testUser2.Lastname = 'user41';
        testUser2.Firstname = 'test41';
        testUser2.Alias = 'test4';
        testUser2.CommunityNickname = '1234641';
        testUser2.UserRole = [ select id from userrole where id ='00EG00000015wJk' ];
        testUser2.ProfileId = '00eG0000000fz9y';
        
        //testUser.CurrencyIsoCode = 'USD';
        testUser2.TimeZoneSidKey = 'GMT';
        testUser2.LocaleSidKey = 'en_US';
        testUser2.EmailEncodingKey = 'ISO-8859-1';
        testUser2.LanguageLocaleKey = 'en_US';
        testUser2.UserPermissionsMobileUser = false;

        insert testUser2;

        Account   testAccount = new Account();
        testAccount.Name  = 'Test Account';
    
        insert testAccount;
    
        Contact   testContact = new Contact();
        testContact.LastName  = 'Test Name';
        
        Test.startTest();

        insert testContact;

        Product2  testProduct = new Product2();
        testProduct.Name                  = 'Test Product Name';
        testProduct.Product_Category__c   = 'Category';
        testProduct.Product_Family__c     = 'Family';
        testProduct.Product_Sub_family__c = 'Sub-Family';    

        insert testProduct;
    
        Case  testCase  = new Case();
        testCase.RecordTypeId = supportCaseRecordType.Id;
        testCase.Summary__c   = 'Summary';
        testCase.Description  = 'Description';
        testCase.Origin       = 'Email';
        testCase.Status       = 'Awaiting Customer Response';
        testCase.I_Agree__c   = true;
        testCase.ContactId    = testContact.Id;
        testCase.ProductId    = testProduct.Id;
        testCase.OwnerId      = testUser.Id;        

        insert testCase;
	stopRecursion.stopRecursiveTriggerRun = false; // Amit added
        testCase.OwnerId = testUser2.Id;  
        update testCase; 

        Test.stopTest();  

        Case c = [SELECT Status,OwnerID FROM Case WHERE Id = :testCase.Id];

        System.assertEquals( testUser2.Id, c.OwnerId );
            
    }
}
sfdc18sfdc18
Thanks Amit.
I am getting 72% code coverage now.
What should I do to cover these lines so that code coverage can become more than 75%
Map<Id, boolean> onlineMap = n2de.DistributionEngineGlobal.getAvailabilityByUser(oId);

      for (Id id : onlineMap.keySet()) {  
      oooFlag = onlineMap.get(id);
       break;
      }
      if(!oooFlag){
      c.OwnerId = Label.Case_ReDistributeCasesId;
      }
These lines are still not covered.

 
AMIT KAMBOJAMIT KAMBOJ
Please use this updated code to boost coverage. Please mark this as solution if this resolves the issue.
@isTest
private class UserAvailabilityCheckTest {

    private  static  RecordType supportCaseRecordType  = [SELECT Name, Id FROM RecordType 
                                                          WHERE Name ='Support Case' 
                                                          AND SObjectType = 'Case'];
    static testMethod void testUserAvailabilityCheck() {
        
        User testUser = new User();
        testUser.Username= 'm98704nn@companyn.com';
        testUser.Email = 'testuser198704nn@companyn.com';
        testUser.Lastname = 'user4';
        testUser.Firstname = 'test4';
        testUser.Alias = 'test4';
        testUser.CommunityNickname = '123464';
        testUser.UserRole = [ select id from userrole where id ='00EG00000015wJk' ];
        testUser.ProfileId = '00eG0000000fz9y';
        
        //testUser.CurrencyIsoCode = 'USD';
        testUser.TimeZoneSidKey = 'GMT';
        testUser.LocaleSidKey = 'en_US';
        testUser.EmailEncodingKey = 'ISO-8859-1';
        testUser.LanguageLocaleKey = 'en_US';
        testUser.UserPermissionsMobileUser = false;

        insert testUser;
        
        User testUser2 = new User();
        testUser2.Username= 'm987041nn@companyn.com';
        testUser2.Email = 'testuser1987041nn@companyn.com';
        testUser2.Lastname = 'user41';
        testUser2.Firstname = 'test41';
        testUser2.Alias = 'test4';
        testUser2.CommunityNickname = '1234641';
        testUser2.UserRole = [ select id from userrole where id ='00EG00000015wJk' ];
        testUser2.ProfileId = '00eG0000000fz9y';
        
        //testUser.CurrencyIsoCode = 'USD';
        testUser2.TimeZoneSidKey = 'GMT';
        testUser2.LocaleSidKey = 'en_US';
        testUser2.EmailEncodingKey = 'ISO-8859-1';
        testUser2.LanguageLocaleKey = 'en_US';
        testUser2.UserPermissionsMobileUser = false;

        insert testUser2;

        Account   testAccount = new Account();
        testAccount.Name  = 'Test Account';
    
        insert testAccount;
    
        Contact   testContact = new Contact();
        testContact.LastName  = 'Test Name';
        
        Test.startTest();

        insert testContact;

        Product2  testProduct = new Product2();
        testProduct.Name                  = 'Test Product Name';
        testProduct.Product_Category__c   = 'Category';
        testProduct.Product_Family__c     = 'Family';
        testProduct.Product_Sub_family__c = 'Sub-Family';    

        insert testProduct;
    
        Case  testCase  = new Case();
        testCase.RecordTypeId = supportCaseRecordType.Id;
        testCase.Summary__c   = 'Summary';
        testCase.Description  = 'Description';
        testCase.Origin       = 'Email';
        testCase.Status       = 'Awaiting Customer Response';
        testCase.I_Agree__c   = true;
        testCase.ContactId    = testContact.Id;
        testCase.ProductId    = testProduct.Id;
        testCase.OwnerId      = testUser.Id;        

        insert testCase;
	stopRecursion.stopRecursiveTriggerRun = false; // Amit added
        testCase.OwnerId = testUser2.Id;
	testCase.Status = 'In Progress';	// Amit added for code coverage
        update testCase; 

        Test.stopTest();  

        Case c = [SELECT Status,OwnerID FROM Case WHERE Id = :testCase.Id];

        System.assertEquals( testUser2.Id, c.OwnerId );
            
    }
}

 
This was selected as the best answer
Cyrus TalladenCyrus Talladen
if ((oldStatusClosed && newStatusInProgress)||(oldStatusACR && newStatusInProgress)||(oldStatusResolved && newStatusInProgress))

may not be returning true in which case the block inside will not be executed.  In this case this statement will only be true if
 
​oldStatus = true
newStatusInProgress = true

OR

oldStatusACR= true
newStatusInProgress= true


OR

oldStatusResolved= true
newStatusInProgress= true

In which any code from the above have not considered

Cyrus Talladen
CRM Engineer
www.levementum.com
sfdc18sfdc18
Thanks Amit, Cyrus.
I have also added below code to my trigger.
 If(Test.IsRunningTest()){
      oooFlag = false;
 }