0

I'm having some problems trying to write a test class.

The issue is, the class which is being tested is composed of 5 methods that receive some information and use that information for querying. They're all very simple, here's an example:

 public List<AgendamentoOrdemServico__c> getAgendamentoOrdemServico(String equipe, Datetime agStartDate, Datetime agEndDate, String tecnico, List<Boolean> lstStatus)
{
    return [Select
                Id,
                Name, 
                Tecnico_DSM__c,
                Tecnico_DSM__r.Name, 
                Agendado_Para__c,
                Previsao_de_Termino__c, 
                Ordem_de_servico_DSM__c,
                Ordem_de_servico_DSM__r.Name,
                Ordem_de_servico_DSM__r.Produto_Instalado__c,
                Ordem_de_servico_DSM__r.Defeito_Reclamado__c,
                Ordem_de_servico_DSM__r.Nome_do_consumidor__c,
                Ordem_de_servico_DSM__r.Telefone_consumidor__c,
                Ordem_de_servico_DSM__r.Endereco__c,
                Ordem_de_servico_DSM__r.Proximidade__c,                     
                Motivo_do_Agendamento__c,
                Atendente_DSM__c, 
                Justificativa_atraso_atendimento__c,
                DescricaoAtividade__c,
                RecordTypeId,
                RecordType.DeveloperName,
                isAtivo__c 
            From 
                AgendamentoOrdemServico__c
            Where
                Tecnico_DSM__r.Service_Group__c =: equipe
                And Agendado_Para__c >=: Datetime.newInstance(agStartDate.year(), agStartDate.month(), agStartDate.day(), 0, 0, 0)
                And Previsao_de_Termino__c <: Datetime.newInstance(agEndDate.year(), agEndDate.month(), agEndDate.day(), 23, 0, 0)
                And Tecnico_DSM__c =: tecnico
                And isAtivo__c IN: lstStatus
            Order by Agendado_Para__c];
}

They all query differently, but that's the idea, receive some information, query and return.

I was not the one who wrote this class and neither am I allowed to mess around with it.

So, I started writing my test class for it, and here's what I got so far:

    @isTest
private class AgendamentoOrdemServicoDAO_DSM_tst {

@TestSetup
public static void setup() {

    // Here I inserted all the relevant data. 
  
}

@isTest
public static void testGetAllAgendamentoOrdemServico() {
    Test.startTest();
  
    Datetime dateToday = Datetime.now().addDays(1);
    AgendamentoOrdemServico__c agendamento = new AgendamentoOrdemServico__c();
    List<AgendamentoOrdemServico__c> allAg = [
        SELECT
                Id,
                Name, 
                Tecnico_DSM__c, 
                Tecnico_DSM__r.Name,
                Agendado_Para__c,
                Previsao_de_Termino__c, 
                Ordem_de_servico_DSM__c,
                Ordem_de_servico_DSM__r.Name,
                Ordem_de_servico_DSM__r.Produto_Instalado__c,
                Ordem_de_servico_DSM__r.Defeito_Reclamado__c,
                Ordem_de_servico_DSM__r.Nome_do_consumidor__c,
                Ordem_de_servico_DSM__r.Telefone_consumidor__c,
                Ordem_de_servico_DSM__r.Endereco__c,
                Ordem_de_servico_DSM__r.Proximidade__c,                 
                Motivo_do_Agendamento__c,
                Atendente_DSM__c, 
                Justificativa_atraso_atendimento__c,
                DescricaoAtividade__c,
                RecordTypeId,
                RecordType.DeveloperName,
                isAtivo__c 
            FROM 
                AgendamentoOrdemServico__c
            WHERE
                Tecnico_DSM__r.Service_Group__c =: [SELECT id FROM Service_Group__c LIMIT 1].id                 
                And Agendado_Para__c >=: dateToday
                And Previsao_de_Termino__c <: Datetime.newInstance(dateToday.year(), dateToday.month(), dateToday.addDays(1).day(), 0, 0 , 0)
                And isAtivo__c =: true
            ORDER by Tecnico_DSM__r.Name, Agendado_Para__c
            ];

            System.assert(allAg.size() > 0, 'Não trouxe agendamento.');
    Test.stopTest();
}

Problem is, IT WORKS, but it doesn't really cover anything. I'm kind of new to test classes, I've done some tests for classes that actually did something other than just querying, and I'm having massive problems getting this one to cover ANYTHING at all.

1 Answer 1

1

You need to call the actual method in your test class so that it tests the actual method rather than recreating the logic in the test class.

for example: in your test class, do not perform the query itself, call the actual method that returns the results and then verify that it returned the right records.. or not (based on whether or not you inserted them in the TestSetup / test methods)

@isTest
private class AgendamentoOrdemServicoDAO_DSM_tst {
    
    @TestSetup
    public static void setup() {
    
         // Here I inserted all the relevant data. 
      
    }
    
    @isTest
    public static void testGetAllAgendamentoOrdemServico() {
        Datetime dateToday = Datetime.now().addDays(1);
        AgendamentoOrdemServico__c agendamento = new AgendamentoOrdemServico__c();
        Test.startTest();
        //call the actual method:
        List<AgendamentoOrdemServico__c> allAg = agendamento.getAgendamentoOrdemServico('equip', 'parameter2', ....);
        Test.stopTest();
        //Make your assertions here
        System.assertEquals(allAg.size(), 1);
    }
}

When you run that test method, it will set up a test execution, insert the data in testSetup and call your method. (will run through all the lines of code in your class) This way you will get the code coverage. It is your job to make test classes that test functionality, so for the queries, you can have several different executions with different parameters and/or data and you should assert that it is returning the correct records.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .