You need to sign in to do that
Don't have an account?
Gorka 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
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
Try this test class, modify it if necessary
The same problem, the code verification stops in the while...