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
Saad Ahmad 27Saad Ahmad 27 

Unable to cover a couple of lines in test class

I have written the following trigger, however I'm unable to cover the following lines in my test class. Unable to figure out what I'm missing. Thank you in advance for everyones help!

User-added image
Here is the trigger:
trigger SetCaseDetails on Case (before insert, before update) {
    Id ITSRecordID = Olympus__c.getOrgDefaults().ITS_Case_Record_Type__c;
    Id TechRecordID = Olympus__c.getOrgDefaults().TechSupportID__c;
    Map<Id, Case> parentCaseOwners = new Map<Id, Case>();
    Map<String, Repair_History__c> repairHistories = new Map<String, Repair_History__c>();
    for (Case c : Trigger.new) {
        if (c.ParentId != null) {
            parentCaseOwners.put(c.ParentId, null);
        }
        if (c.SO__c != null) {
            repairHistories.put(c.SO__c, null);
        } if (c.Status == 'Closed' || c.Status == 'No Bid' || c.Status == 'Complete' || c.Status == 'Cancelled') {
            c.Open_Lookup__c = null;
       } else if (c.AccountId != null) {
            c.Open_Lookup__c = c.AccountId;
        }
    }    
    if (!parentCaseOwners.isEmpty()) {
        for (Case pc: [SELECT Id, OwnerId FROM Case WHERE Id in :parentCaseOwners.keySet()]) {
            parentCaseOwners.put(pc.Id,pc);
        }        
        for (Case c : Trigger.new) {
            if (c.ParentId != null & c.RecordTypeID != ITSRecordID & c.RecordTypeID != TechRecordID) {
                c.OwnerId = parentCaseOwners.get(c.ParentId).OwnerId;
            }
        }
    }   
    if (!repairHistories.isEmpty()) {
        for (Repair_History__c rh : [SELECT Id, Account__c, Repair_Number__c, Product__c FROM Repair_History__c WHERE Repair_Number__c in : repairHistories.keySet()]) {
            repairHistories.put(rh.Repair_Number__c, rh);
        }        
        for (Case c : Trigger.new) {
            if (c.SO__c != null && repairHistories.get(c.SO__c) != null) {
                c.Repair_History__c = repairHistories.get(c.SO__c).Id;
                c.ProductId = repairHistories.get(c.SO__c).Product__c;
                if (c.AccountId == null && repairHistories.get(c.SO__c).Account__c != null) {
                    c.AccountId = repairHistories.get(c.SO__c).Account__c;
                }
            }
        }
    }
}

Here is the test class:
 
@isTest
private class TestSetCaseDetails {
  static testMethod void test() {       
        List<Repair_History__c> repairhistories = new List<Repair_History__c>();
        List<Case> caseowners = new List<Case>();
      
      	Map<id,Case> newMap = new Map<id,Case>();
      
      	Id caserec = Schema.SObjectType.Case.getRecordTypeInfosByName().get('BA Support Case').getRecordTypeId();
      
        Account a1 = new Account(
            Name='A1',
            BillingStreet = 'Street',
            BillingCity = 'City',
            BillingPostalCode = 'Postal Code',
            BillingState = 'ON',
            BillingCountry = 'Country'        
        );
        insert a1;
        
        case c1 = new Case(
        	Status='New',
        	Origin='Email',
            subject='parent case',
            Open_Lookup__c=a1.id,
            accountid=a1.id
        	);
        insert c1;
      newMap.put(c1.id,null);
      caseowners.add(c1);
      
     	case c2 = new Case(
        	Status='closed',
        	Origin='Email',
            subject='child case',
            parent=caseowners[0],
            SO__c='12345678',
            RecordTypeId=caserec
        	);
        insert c2;
      newMap.put(c2.id,null);
      
      case c3 = new Case(
        	Status='closed',
        	Origin='Email',
            subject='child case',
            parent=caseowners[0],
            SO__c='12345678'
        	);
        insert c3;
      
      caseowners.add(c2);
      
        Account a2 = new Account(
            Name = 'A2',
            JDE_Account_Number__c='JDE',
            ParentId=a1.Id,
            BillingStreet = 'Street',
            BillingCity = 'City',
            BillingPostalCode = 'Postal Code',
            BillingState = 'ON',
            BillingCountry = 'Country'
        );
        insert a2;
        
      	Repair_History__c p1 = new Repair_History__c();
            p1.Repair_Number__c  = '123456';
            p1.Total_Price__c  = 100;
            p1.Account__c = a1.Id;
            insert p1;
      
      	Repair_History__c p2 = new Repair_History__c();
            p2.Repair_Number__c  = '1234567';
            p2.Total_Price__c  = 100;
            p2.Account__c = a2.Id;
            insert p2;
      
      	repairhistories.add(p1);
        repairhistories.add(p2);
      
    }

}

​​​​​​​
 
Best Answer chosen by Saad Ahmad 27
Raj VakatiRaj Vakati
Try this code
 
@isTest
private class TestSetCaseDetails {
  static testMethod void test() {    


 Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
System.runAs(uu){
		
        List<Repair_History__c> repairhistories = new List<Repair_History__c>();
        List<Case> caseowners = new List<Case>();
      
      	Map<id,Case> newMap = new Map<id,Case>();
      
      	Id caserec = Schema.SObjectType.Case.getRecordTypeInfosByName().get('BA Support Case').getRecordTypeId();
      
        Account a1 = new Account(
            Name='A1',
            BillingStreet = 'Street',
            BillingCity = 'City',
            BillingPostalCode = 'Postal Code',
            BillingState = 'ON',
            BillingCountry = 'Country'        
        );
        insert a1;
        
        case c1 = new Case(
        	Status='New',
        	Origin='Email',
			SO__c='12345678',
            subject='parent case',
            Open_Lookup__c=a1.id,
            accountid=a1.id
        	);
        insert c1;
      newMap.put(c1.id,null);
      caseowners.add(c1);
      
     	case c2 = new Case(
        	Status='closed',
        	Origin='Email',
            subject='child case',
			
            Parent=c1.Id,
            SO__c='12345678',
            RecordTypeId=caserec
        	);
        insert c2;
      newMap.put(c2.id,null);
      
      case c3 = new Case(
        	Status='closed',
        	Origin='Email',
            subject='child case',
            Parent=c1.Id,
            SO__c='12345678'
        	);
        insert c3;
      
      caseowners.add(c2);
      
        Account a2 = new Account(
            Name = 'A2',
            JDE_Account_Number__c='JDE',
            ParentId=a1.Id,
            BillingStreet = 'Street',
            BillingCity = 'City',
            BillingPostalCode = 'Postal Code',
            BillingState = 'ON',
            BillingCountry = 'Country'
        );
        insert a2;
        
		
		 Product2 prod = new Product2(Name = 'Laptop X200', 
                                     Family = 'Hardware');
        insert prod;
        
		
		
      	Repair_History__c p1 = new Repair_History__c();
            p1.Repair_Number__c  = '123456';
            p1.Total_Price__c  = 100;
            p1.Account__c = a1.Id;
			 p1.product__c = prod.Id;
            insert p1;
      
      	Repair_History__c p2 = new Repair_History__c();
            p2.Repair_Number__c  = '1234567';
            p2.Total_Price__c  = 100;
            p2.Account__c = a2.Id;
				 p2.product__c = prod.Id;
            insert p2;
      
      	repairhistories.add(p1);
        repairhistories.add(p2);
      
    }
  }
}