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
SFDC_shubhSFDC_shubh 

trigger test class assert error

Write a trigger to add Mr. and Mrs. as a prefix on Account Name on the basis of sex checkbox & radio button. ?
trigger accNamePrefix on Account (before insert, before update) {
for (Account acc : trigger.new){
    String name = acc.Name;
    if(acc.Sex__c == 'Male' && !name.contains('Mr.')){
        acc.Name = 'Mr. '+acc.Name;
    }
    else if(!name.contains('Mrs.')){
        acc.Name = 'Mrs. '+acc.Name;
    }
   }
}

and its test class--
@istest
public class namePrefixTest {
    @istest
    public static void method(){
        Account acc = new Account();
        acc.Name = 'sss';
        acc.AccountNumber = '12';
        acc.Sex__c = 'Male';
        insert acc;
        acc = [Select Id, Name FROM Account];
        acc.Name = 'sds';
        update acc;
        System.assert(acc.Name.contains('Mr.'));
    }
}

assertion failed in this methd, So what to do in this class, assert is pass
Best Answer chosen by SFDC_shubh
Raj VakatiRaj Vakati
This is 100 % code coverage 
 
@istest
public class namePrefixTest {
    @istest
    public static void method(){
        Account acc = new Account();
        acc.Name = 'sss';
        acc.AccountNumber = '12';
        acc.Sex__c = 'Male';
        insert acc;
        acc = [Select Id, Name FROM Account];
        acc.Name = 'sds';
        update acc;
        
        Account a = [Select Name From Account Limit 1 ];
        System.assert(a.Name.contains('Mr.'));
    }
     @istest
    public static void femTest(){
        Account acc = new Account();
        acc.Name = 'sss';
        acc.AccountNumber = '12';
        acc.Sex__c = 'Female';
        insert acc;
        acc = [Select Id, Name FROM Account];
        acc.Name = 'sds';
        update acc;
        
        Account a = [Select Name From Account Limit 1 ];
        System.assert(a.Name.contains('Mrs.'));
    }
}

 

All Answers

Raj VakatiRaj Vakati
Try this
 
@istest
public class namePrefixTest {
    @istest
    public static void method(){
        Account acc = new Account();
        acc.Name = 'sss';
        acc.AccountNumber = '12';
        acc.Sex__c = 'Male';
        insert acc;
		acc = [Select Id, Name FROM Account];
        acc.Name = 'sds';
        update acc;
		
		Account a = [Select Name From Account Limit 1 ];
        System.assert(a.Name.contains('Mr.'));
    }
}

 
Raj VakatiRaj Vakati
This is 100 % code coverage 
 
@istest
public class namePrefixTest {
    @istest
    public static void method(){
        Account acc = new Account();
        acc.Name = 'sss';
        acc.AccountNumber = '12';
        acc.Sex__c = 'Male';
        insert acc;
        acc = [Select Id, Name FROM Account];
        acc.Name = 'sds';
        update acc;
        
        Account a = [Select Name From Account Limit 1 ];
        System.assert(a.Name.contains('Mr.'));
    }
     @istest
    public static void femTest(){
        Account acc = new Account();
        acc.Name = 'sss';
        acc.AccountNumber = '12';
        acc.Sex__c = 'Female';
        insert acc;
        acc = [Select Id, Name FROM Account];
        acc.Name = 'sds';
        update acc;
        
        Account a = [Select Name From Account Limit 1 ];
        System.assert(a.Name.contains('Mrs.'));
    }
}

 
This was selected as the best answer
SFDC_shubhSFDC_shubh
thanks...@raj
Norma iuytNorma iuyt
Thanks for the information.
kissanime (https://www.kissanime.vip/)