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
Gorka SanzGorka Sanz 

test apex class with while

Hi to all.

I develop a class that search certain Cases and change the owner and another fields.

I debug it and works without problems. But when i create a test class, it doesn't work.

Here is my code:
public class Casos{
    @InvocableMethod
    public static void escalarCasos (){
        datetime dt = system.now();
        date Hoy= system.today();
        //Comprobar si el dia de la semana es sabado o domingo. Si lo es no se escala
        string dia = dt.format('E');
        
        if ((dia!='Sat') && (dia!='Sun')){
            //el dia es escalable
            //como es un dia escalable, ver si es un dia festivo
            integer count= [SELECT COUNT() from FESTIVO__c where Fecha_Festivo__c=:Hoy];
            if (count ==0)
            {
                //no es un dia festivo, por lo que hay que traer los casos
                List<Case> cases = new list<Case>();
                //cases = [SELECT Id, OwnerId, Status, Fecha_Asignacion__c, Type, Nivel_1__c, Nivel_2__c, Nivel_3__c,Nivel_4__c, Nivel_5__c FROM Case WHERE (Status = 'Nuevo' or Status = 'Reanudado') and (Fecha_Asignacion__c !=null)];
                
                cases = [SELECT Id, OwnerId, Status, Fecha_Asignacion__c, Type, Nivel_1__c, Nivel_2__c, Nivel_3__c,Nivel_4__c, Nivel_5__c, Nivel_asignacion__c FROM Case 
                         WHERE (Status = 'Nuevo' or Status = 'Reanudado') and (Fecha_Asignacion__c !=null) and (Nivel_Asignacion__c !=null)
                         and  Fecha_Asignacion__c<:dt - 1];
                count=0;//el contador empieza en cero para las listas
                while (cases.size()>count)
                {
                    //system.debug(cases[count].Fecha_Asignacion__c);
                    string vTipo = cases[count].Type;
                    string vNivel1 = cases[count].Nivel_1__c;
                    string vNivel2 = cases[count].Nivel_2__c;
                    string vNivel3 = cases[count].Nivel_3__c;
                    string vNivel4 = cases[count].Nivel_4__c;
                    string vNivel5 = cases[count].Nivel_5__c;
                    decimal vNivelAsg = cases[count].Nivel_asignacion__c;
                    decimal asig;
                    asig = [Select count() from Asignacion__c where Tipo__c=:vTipo 
                                   and Nivel_1__c=:vNivel1
                                   and Nivel_2__c=:vNivel2 and Nivel_3__c=:vNivel3
                                   and Nivel_4__c=:vNivel4 and Nivel_5__c=:vNivel5
                                   and Nivel_asignacion__c=:vNivelAsg + 1];
                    //system.debug('Conteo Asignacion = ' + asig);
                    if (asig!=0)
                    {
                        //se encontro un nivel
                        string usuario = [select Usuario__c from Asignacion__c
                                         where Tipo__c=:vTipo 
                                   and Nivel_1__c=:vNivel1
                                   and Nivel_2__c=:vNivel2 and Nivel_3__c=:vNivel3
                                   and Nivel_4__c=:vNivel4 and Nivel_5__c=:vNivel5
                                   and Nivel_asignacion__c=:vNivelAsg + 1 limit 1].Usuario__c;
                        //system.debug(usuario);
                        // sacar el identificativo de la funcion de usuario
                        string id_funcion = [Select id, SobjectType, QueueId, Queue.Name from QueueSobject
                                            where Queue.Name=:usuario].QueueId;
                        //aqui hay que hacer update de el ID de owner, el nivel de asignacion y la fecha de asignacion
                        cases[count].Fecha_Asignacion__c=system.now();
                        cases[count].Nivel_Asignacion__c=vNivelAsg + 1;
                        cases[count].OwnerId=id_funcion;
                        cases[count].Es_reanudado__c=true;
                    }
                    count++;
                }
                update cases;

            }
        }
    }
}

and here my test class:
@isTest
private class TestEscalarCasos {
    @isTest static void escalarCasosTest() {
        Casos.escalarCasos();
    }   
}

When i saw the code coverage, always is 31 %, and never go pas the while statment. I'm new in salesforce, what is the problem?

Thanks
Steven NsubugaSteven Nsubuga
You have to create some data for use within the test.
Try this test class, modify it if necessary
@isTest
private class TestEscalarCasos {
    @isTest static void escalarCasosTest() {
		
				
		List<Case> cases = new list<Case>();
		for (Integer i = 1; i < 5; i++){
			Case c = new Case();
			c.Status = 'Nuevo';
			c.Fecha_Asignacion__c = date.today().addDays(-2);
			c.Nivel_Asignacion__c = 'Nuevo';
			c.Type = 'Other';
			c.Nivel_1__c = 'Nuevo1';
			c.Nivel_2__c = 'Nuevo2';
			c.Nivel_3__c = 'Nuevo3';
			c.Nivel_4__c = 'Nuevo4';
			c.Nivel_5__c = 'Nuevo5';
			cases.add(c);		
		}
		insert cases;
		
		Asignacion__c asign = new Asignacion__c;
		asign.Tipo__c = 'Other';
		asign.Nivel_1__c = 'Nuevo1';
		asign.Nivel_2__c = 'Nuevo2';
		asign.Nivel_3__c = 'Nuevo3';
		asign.Nivel_4__c = 'Nuevo4';
		asign.Nivel_5__c = 'Nuevo5';
		insert asign;
		
        Casos.escalarCasos();
    }   
}


 
Gorka SanzGorka Sanz
Doesn´t work :(

The same problem, the code verification stops in the while...