2

I'm having a hard time covering the condition in my class wherein it is checking for a specific record type name. In my test class, I've already inserted the record type desired, but there's a portion which isn't covered.

//portion of apex class not covered

if(customA.RecordType.Name == 'Record Type 1') {
    //some code.....
}

Meanwhile, below is the test class:

@isTest
public class MyClassTest{

     public static testMethod void MyClassTest(){

        List<CustomA__c> aList = new List<CustomA__c>();
        CustomA__c a1 = new CustomA__c();    
        a1.Name = 'A1';
        a1.RecordTypeId = Schema.SObjectType.CustomA__c.getRecordTypeInfosByName().get('Record Type 1').getRecordTypeId(); 

        List<CustomA__c> aList = new List<CustomA__c>();
        CustomA__c a2 = new CustomA__c();    
        a2.Name = 'A2';
        a2.RecordTypeId = Schema.SObjectType.CustomA__c.getRecordTypeInfosByName().get('Record Type 2').getRecordTypeId(); 

        aList.add(a1);
        aList.add(a2);

        insert aList;

        List<CustomB__c> bList = new List<CustomB__c>();
        CustomB__c b1 = new CustomB__c();
        b1.CustomA__c = a1.Id;
        b1.RecordTypeId = Schema.SObjectType.CustomB__c.getRecordTypeInfosByName().get('Record Type 1').getRecordTypeId();
        b1.Number__c = 10;

        List<CustomB__c> bList = new List<CustomB__c>();
        CustomB__c b2 = new CustomB__c();
        b2.CustomA__c = a2.Id;
        b2.RecordTypeId = Schema.SObjectType.CustomB__c.getRecordTypeInfosByName().get('Record Type 2').getRecordTypeId();
        b2.Number__c = 10;

        bList.add(b1); 
        bList.add(b2);

        insert bList;

        b1.Number__c = 20;
        b2.Number__c = 20;

        update bList;

        CustomA__c a1AfterInsert = [SELECT Total_Number__c FROM CustomA__c WHERE Id =: a1.Id];

        system.assertequals(20, a1AfterInsert.Total_Number__c);

        system.debug('>>a1TotalNumber>>' + a1AfterInsert.Total_Number__c);

        MyClass mc = new MyClass();
     }

}

1 Answer 1

6

I think you're referencing an SObjectField token. Make sure you're referencing a record's record type name:

if(someRecord.RecordType.Name == 'Record Type 1') {
  // ...

Using the other form is technically allowed, but will never be true:

if(Account.RecordType.Name == 'Record Type 1') { // Always false
2
  • Apologies for the typo. In my actual class, I am really referencing the record's record type name in the condition. I have modified the code posted.
    – asdfg
    Mar 27, 2018 at 8:23
  • I have made sure I created first CustomA__c record with Record Type 1 record type, but still, portion from the controller isn't covered.
    – asdfg
    Mar 27, 2018 at 8:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .