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
EvertonSzekeresEvertonSzekeres 

Writing test class

Can you guys help me to write a test class?

My trigger:

trigger Atualizar_Fotosfaltantes_AR_case on Account (after update) {

   public List<Case> casesToUpdate = new List<Case>();
   public List<Task> tasksToUpdate = new List<Task>();
    
   public Set <id> ids = new Set <id> ();
    
    for (Account acc: trigger.new) {
        if (acc.antecipado__c == 'AR' || acc.antecipado__c == 'TES' || acc.Nome_do_produto_adquirido__c.contains('AR ')){
            ids.add(acc.id);
        }
    
    if(ids.size()>0){
    For(Case c : [SELECT Id, motivo__c, Formando_com_fotos__c, accountid FROM Case WHERE (motivo__c = 'Formandos AR' OR motivo__c = 'Fotos faltantes') AND accountId IN: ids]){
    For(Task t : [SELECT Id, subject, status FROM Task WHERE (subject = 'AR - Aguardando fotos faltantes' OR subject = 'AR - LuxColor - Aguardando fotos faltantes') AND status != 'Concluído' AND whatid =: c.id]){
    
            if((acc.Formando_com_novas_fotos__c == 'SIM' || acc.Formando_com_novas_fotos__c == 'NÃO') && c.motivo__c == 'Formandos AR'){        
                c.Formando_com_fotos__c = acc.Formando_com_novas_fotos__c;
                }
            if((acc.Formando_com_novas_fotos__c == 'SIM' || acc.Formando_com_novas_fotos__c == 'NÃO') && c.motivo__c == 'Formandos AR'){        
                t.status = 'Concluído';
                
            }
            if((acc.Formando_com_novas_fotos__c == 'FOTOS RECEBIDAS' || acc.Formando_com_novas_fotos__c == 'ENVIAR NOVAMENTE') && c.motivo__c == 'Fotos faltantes'){        
                c.Formando_com_fotos__c = acc.Formando_com_novas_fotos__c; 
            }
                casesToUpdate.add(c);
                tasksToUpdate.add(t); 
            }
    }
    }
    }
    	update casesToUpdate;
    update taskstoupdate;
}

I tried create the test class, but it stops to coverage in line 14 of my trigger so I stoped to continue in my test class. I got 52%:

@isTest(SeeAllData=true)
public class FotosFaltantes_AR_test{
  static testmethod void MyUnitTest001(){

      Account acc = new Account();
            acc.LastName = 'Account Avulso';
            acc.recordtypeid = '012U00000000vYx';
      		acc.Antecipado__c = 'AR';
   			acc.Formando_com_novas_fotos__c = null;
        insert acc;
      
       Test.startTest();
          
           acc.Formando_com_novas_fotos__c = 'SIM';
    update acc;
      
      Test.stopTest();

    Case c = new Case();
      	c.motivo__c = 'Formandos AR';
        c.Formando_com_fotos__c = null;
      	c.status = 'Novo';
      c.AccountId = acc.id;
      insert c;
      
    Task t = new Task();
      	t.Subject = 'AR - Aguardando fotos faltantes';
      	t.Status = 'Nenhum';
      t.WhatId = c.id;
      insert t;
     

      	List<Case> cases = [SELECT Formando_com_fotos__c FROM Case WHERE Id =:c.Id];
       System.assertEquals(acc.Formando_com_novas_fotos__c, cases[0].Formando_com_fotos__c);
  }
}

Thanks !
Sunil02KumarSunil02Kumar
Hi Everton,

Please find below test class.  This will cover your line 14.

@isTest(SeeAllData=true)
public class FotosFaltantes_AR_test{
  static testmethod void MyUnitTest001(){

      Account acc = new Account();
            acc.LastName = 'Account Avulso';
            acc.recordtypeid = '012U00000000vYx';
        acc.Antecipado__c = 'AR';
      acc.Formando_com_novas_fotos__c = null;
        insert acc;
 
  Case c = new Case();
       c.motivo__c = 'Formandos AR';
        c.Formando_com_fotos__c = null;
       c.status = 'Novo';
  c.AccountId = acc.id;
  insert c;
 
  Task t = new Task();
       t.Subject = 'AR - Aguardando fotos faltantes';
       t.Status = 'Nenhum';
        t.WhatId = c.id;
        insert t;
     
       Test.startTest();
         
        acc.Formando_com_novas_fotos__c = 'SIM';
        update acc;
        acc.Formando_com_novas_fotos__c ='FOTOS RECEBIDAS'
        update acc;
      Test.stopTest();
  List<Case> cases = [SELECT Formando_com_fotos__c FROM Case WHERE Id =:c.Id];
       System.assertEquals(acc.Formando_com_novas_fotos__c, cases[0].Formando_com_fotos__c);
  }
}

Few suggestion:
  • While creating test class, create test data in test class itself. Make you test class organization independent. So avoid using SeeAllData=true.
  • While writing test class, always do DML operation on multiple records(minimum-20 records) instead of single records.
  • Avoid SOQL query within For loop to avoid Governor limits
Hope this will help you.
[If it solves your problem, please mark it as solution]

Thanks,
Sunil Kumar
EvertonSzekeresEvertonSzekeres
Thanks Kumar!

Didn't worked.

Now I got 0%.

Is there another way to update 2 objects like in my trigger without using SOQL query in For loop?

Thanks again!
Sunil02KumarSunil02Kumar
Hi Everton,

Actually in test class, you were updating the account before inserting related case and task records. Thats was reason it was not going into for loop.

Insert all records firsts and update account.

Also update case and task records outside the for loop.

I don't know why test class is not giving coverage but it should. 

Thanks,
Sunil Kumar
EvertonSzekeresEvertonSzekeres
I don't know If this is related, but...

I have a webservice callout in my account.
I'm facing troubles in all tests class and Triggers that involving my accounts.

Is this in somehow related?