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
aishwarya kutteaishwarya kutte 

Test class i want 100% code coverage

Hello everyone I write all trigger event on Account object...and test class for the same....i am getting 96% code coverage but i want 100% code coverage .....i am new learner, please help me in this.....any help is appriatiated ...


this is the the Trigger
trigger SelfAccountTrigger on Account(before delete, before insert, before update,after delete,after insert,after update){
    
 //Before Insert logic
    
if(Trigger.isBefore){
   if(Trigger.isInsert)
   {
   for(Account Acc:Trigger.new)        
   {
     if(Trigger.isInsert && Acc.email__c == NUll)
       Acc.Name.addError('You Cannot Create Account without email');
       //Acc.Name = 'ER'+Acc.Name;
       
   }
 }
    
//Before Update logic
 
  else if(Trigger.isupdate)
 {
   for(Account Acc:Trigger.new)
   {
       Acc.Name = 'ER'+Acc.Name;
    
 }

 }

//Before Delete logic
  
else if(Trigger.isdelete)
{
  for(Account Acc: Trigger.old)
  {
        if(String.isNotBlank(Acc.Description))
        {
            Acc.AddError('Cannot Delete if Account if Description is Available');
        }
    }
}}
  
    
//After Insert and After Update logic
     
else {
    if(Trigger.isInsert){

    List<Contact> ct = new List <Contact>();

    for(Account acc : trigger.new){

        Contact c = new Contact(LastName = acc.name,
                    AccountId=acc.id,
                    Phone=acc.Phone);
                   
        ct.add(c);
    }
    insert ct;
    }
    
    
 //After delete 
else if(Trigger.isdelete)
{

 Messaging.reserveSingleEmailCapacity(trigger.size);
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    for (Account acct :(List<Account>)Trigger.old) {
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        email.setToAddresses(new String[] {'aishwarya_kutte@persistent.com'});
        email.setSubject('Deleted Account Alert');
        email.setPlainTextBody('This message is to alert you that the account named ' + acct.Name + ' has been deleted.');
        emails.add(email);
    }
    Messaging.sendEmail(emails);
}

 }

}





and test class for same 

@isTest
    
  public class SelfAccountTestClass {
  static testmethod void myTest(){
           
          Boolean result =false;
          Account acc = new Account(); 
          acc.Name = 'Test Account';
          acc.email__c = 'Test@mail.com';
         // acc.Description='a'; 
          insert acc;
      
            try{
      Account acc1=new account();
      acc1.Name= 'riya';
      acc1.email__c='riyakutte123@gmail.com';
                
      insert acc1;
      }
      catch(DMLException ex)
      {
      result=true;
      system.assert(result);
      }
   
          acc.Name = 'Test Account 1'; 
          update acc;

          
          acc.Description = '';
          delete acc;
          
            
        }
  }




please help ,me to get 100% code coverage
 
Mayur Gore 10Mayur Gore 10
@isTest
public class SelfAccountTrigger_test
 {
    static testmethod void myTest()
    {
      Boolean result =false;
    
      Account acc1=new account();
      acc1.Name= 'riya';
      acc1.email__c='riyakutte123@gmail.com';
    
      try
       {
          insert acc1;
       }
      catch(DMLException ex)
       {
          result=true;
          system.assert(result);
       }
    
       Account acc = new Account(); 
       acc.Name = 'Test Account';
       acc.email__c = '';
    
       try
       {
         insert acc;   
       }
       catch(Exception e)
       {
       string expetd='You Cannot Create Account without email'; 
       string actual= e.getMessage();
       system.assert(actual.contains(expetd));
       }
    
      acc1.Name = 'Test Account 1'; 
      acc1.Description = 'Winner'; 
      update acc1;
    
      try
      {
       delete acc1;
      }
      catch(Exception e)
      {
      string expetd1='Cannot Delete if Account if Description is Available'; 
      string actual1= e.getMessage();
      system.assert(actual1.contains(expetd1));
      } 
      acc1.Description = '';    
      update acc1;
      delete acc1;
    }
}