+ Start a Discussion
KyoKyo 

AddError in Test Class not work!

I'm not write test class is full loop. Because ploblem adderror method.

 

trigger RelationDuplicationA2A on Relationship__c(before insert) {
set<ID> setAccountID = new set<ID>();
set<ID> setRevAccountID = new set<ID>();
integer setRecordType;

for(Relationship__c ra: Trigger.new){
   if(ra.Account_ID__r.Mark_for_Delete__c != true || ra.Rev_Account_ID__r.Mark_for_Delete__c  != true && (ra.RecordTypeID == '01290000000Ym1W')){
       if (Trigger.new[0].Account_ID__c != NULL && Trigger.new[0].Rev_Account_ID__c != NULL)  {
           setAccountID.Add(ra.Account_ID__c);  //Set Value Relation Account to Account
           setRevAccountID.Add(ra.Rev_Account_ID__c);
           setRecordType = 1;  //set value criteria
       }  
   }
    
}
//------------------------------------------- End Add Data --------------------------------------------------------------------------------------------------

// SOQL for Duplicate Relationship without Loop
    if(setRecordType == 1){
           List<Relationship__c> rs = [ select id,Account_ID__c,Rev_Account_ID__c,Mark_for_Delete__c from Relationship__c  WHERE Account_ID__c = :Trigger.new[0].Account_ID__c And Rev_Account_ID__c =: trigger.new[0].Rev_Account_ID__c And Mark_for_Delete__c =: false];
               
               if (rs.size() > 0 ) {
                   Trigger.new[0].Account_ID__c.addError('Duplicate Relationship');
                   Trigger.new[0].Rev_Account_ID__c.addError('Duplicate Relationship');
               }
     }
}

 Ploblem Trigger Line : 

Trigger.new[0].Account_ID__c.addError('Duplicate Relationship');                 Trigger.new[0].Rev_Account_ID__c.addError('Duplicate Relationship');

 

@isTest
private class TestRelationDuplicationA2A  {
   static testMethod void myUnitTest() {
      Account Acc = new Account(Name = 'test',AccountNumber = '00000001');
         insert Acc;
      Contact Con = new Contact(LastName = 'test',AccountID = Acc.id);
         insert con;
      
      Relationship__c ra = new Relationship__c(Account_ID__c = Acc.id,Rev_Account_ID__c = Acc.id,Mark_for_Delete__c = False);
         try{      
            insert ra;
            }catch(DMLException de){

        }  
      Relationship__c Dup = new Relationship__c(Account_ID__c = Acc.id,Rev_Account_ID__c = Acc.id,Mark_for_Delete__c = False);
             try {
             insert Dup;
             }
             catch (Exception e) {
                   System.debug('Duplicate Relationship');                        
             } 
  }
}

 Thank you so much.


Shashikant SharmaShashikant Sharma

There seems nothing wrong in your code , have you checked that you are reachibg to the code  addError, it might be a posibility that this condition if(setRecordType == 1){ is not satisfied in your test method as I don't see any record type set in test method. SO because of this your code never reached to adderror part in test context.

Try with setting record type for the records that you are inserting in test method.