0

So, I did a button linked to an apex class which turns a candidate into an account when a recruiter wants, when I try to deploy it on production it says that I have 72% of test coverage, but when I run the test in my developer console (on sandbox) it doesn't show the lines in blue or red to help me improve the percentage. Please take a look on the codes and help me.

The Class:

global with sharing class ContratarCandidato{

global ContratarCandidato(){}

@TestVisible
WebService static void gerarContaFromCandidato (Id idCandidato){

    system.Debug(idCandidato);

    List<SFDC_Candidate__c> lstCandidato = new List<SFDC_Candidate__c>([
        SELECT Sobrenome_do_candidato__c, Name, CPF__c, Genero__c, Telefone__c, Celular__c, Email__c, Email_alternativo__c,
               Endereco__c, Bairro__c, Cidade__c, Estado__c, CEP__c, Indicacao_funcionario__c, Id, Origem_da_conta__c
        FROM SFDC_Candidate__c
        WHERE Id = :idCandidato]);            
    system.Debug(lstCandidato);

    Account newAccount= new Account();

    if(lstCandidato[0].Sobrenome_do_candidato__c == null){
        newAccount.LastName = 'N/A';
    }else{
        newAccount.LastName = lstCandidato[0].Sobrenome_do_candidato__c;
    }
    if(lstCandidato[0].Name== null){
        newAccount.FirstName = 'N/A';
    }else{
        newAccount.FirstName = lstCandidato[0].Name;
    }
    if(lstCandidato[0].CPF__c== null){
        newAccount.CPF__pc= 'N/A';
    }else{
        newAccount.CPF__pc= lstCandidato[0].CPF__c;
    }
    if(lstCandidato[0].Genero__c == 'Feminino'){
        newAccount.Salutation = 'Sra.';
    }
    if(lstCandidato[0].Genero__c == 'Masculino'){
        newAccount.Salutation = 'Sr.';
    }
    if(lstCandidato[0].Telefone__c != null){
        newAccount.Phone = lstCandidato[0].Telefone__c;
    }
    if(lstCandidato[0].Celular__c != null){
        newAccount.PersonMobilePhone = lstCandidato[0].Celular__c;
    }
    if(lstCandidato[0].Email__c != null){
        newAccount.PersonEmail = lstCandidato[0].Email__c;
    }
    if(lstCandidato[0].Endereco__c != null){
        newAccount.PersonMailingStreet = lstCandidato[0].Endereco__c;
    }
    if(lstCandidato[0].Bairro__c != null){
        newAccount.Bairro_de_correspondencia__pc = lstCandidato[0].Bairro__c;
    }
    if(lstCandidato[0].Cidade__c != null){
        newAccount.PersonMailingCity = lstCandidato[0].Cidade__c;
    }
    if (lstCandidato[0].Estado__c != null){
        newAccount.PersonMailingState = lstCandidato[0].Estado__c;
    }
    if (lstCandidato[0].CEP__c != null){
        newAccount.PersonMailingPostalCode = lstCandidato[0].CEP__c;
    }
    if (lstCandidato[0].Origem_da_conta__c != null){
        newAccount.AccountSource = lstCandidato[0].Origem_da_conta__c;
    }
    newAccount.Status_da_conta__c = 'Ativa';
    newAccount.Type = 'Colaborador PJ';
    newAccount.Colaborador_RCS__c = true;
    newAccount.RecordTypeId = '012q00000000STyAAM';

    insert newAccount;

    }
}

The Test:

@isTest
public class ContratarCandidatoTest {

static testMethod void gerarContaFromCandidatoTest(){

    SFDC_Candidate__c newCandidato = new SFDC_Candidate__c();        
    newCandidato.Sobrenome_do_candidato__c = 'Class';
    newCandidato.Name = 'Test';
    newCandidato.CPF__c = '980.091.918-22';
    newCandidato.Telefone__c = '(19) 3910-0295';
    newCandidato.Celular__c = '(19) 98940-0982';
    newCandidato.Email__c = 'testclass12@rcs.com.br';
    newCandidato.Email_alternativo__c = 'emailalternativo@rcs.com.br';
    newCandidato.Endereco__c = 'Avenida Andromêda, 885';
    newCandidato.Estado__c = 'São Paulo';
    newCandidato.Bairro__c = 'Alphaville';
    newCandidato.Cidade__c = 'Barueri';

    newCandidato.CEP__c = '86000-000';        
    insert newCandidato;

    newCandidato.Celular__c = '(19) 91234-5678';
    update newCandidato;

    List<SFDC_Candidate__c> lstCandidato = new List<SFDC_Candidate__c>([
        SELECT Sobrenome_do_candidato__c, Name, CPF__c, Telefone__c, Celular__c, Email__c,
               Email_alternativo__c, Endereco__c, Estado__c, Bairro__c, Cidade__c, CEP__c, Genero__c
        FROM SFDC_Candidate__c
        WHERE Name = 'Test'
    ]);

    List<RecordType> newRecordType = new List<RecordType>([
        SELECT Name, Id, SobjectType
        FROM RecordType
        WHERE Name = 'Colaborador RCS'
        AND SObjectType = 'Account']);

    Account newAccount = new Account();        
    if(lstCandidato[0].Sobrenome_do_candidato__c == null){
        newAccount.LastName = 'N/A';
    }else{
        newAccount.LastName = newCandidato.Sobrenome_do_candidato__c;
    }
    if(lstCandidato[0].Name== null){
        newAccount.FirstName = 'N/A';
    }else{
        newAccount.FirstName = newCandidato.Name;
    }
    if(lstCandidato[0].CPF__c== null){
        newAccount.CPF__pc= 'N/A';
    }else{
        newAccount.CPF__pc= newCandidato.CPF__c;
    }
    if(lstCandidato[0].Genero__c == 'Feminino'){
        newAccount.Salutation = 'Sra.';
    }else{
        newAccount.Salutation = 'Sr.';
    }

    if(lstCandidato[0].Telefone__c != null){
        newAccount.Phone = newCandidato.Telefone__c;
    }
    if(lstCandidato[0].Celular__c != null){
        newAccount.PersonMobilePhone = newCandidato.Celular__c;
    }
    if(lstCandidato[0].Email__c != null){
        newAccount.PersonEmail= newCandidato.Email__c;
    }
    if(lstCandidato[0].Endereco__c != null){
        newAccount.PersonMailingStreet = newCandidato.Endereco__c;
    }
    newAccount.Bairro_de_correspondencia__pc = newCandidato.Bairro__c;
    if(newCandidato.Cidade__c != null){
        newAccount.PersonMailingCity = newCandidato.Cidade__c;
    }
    if (newCandidato.Estado__c != null){
        newAccount.PersonMailingState = newCandidato.Estado__c;
    }
    if (newCandidato.CEP__c != null){
        newAccount.PersonMailingPostalCode = newCandidato.CEP__c;
    }
    newAccount.Status_da_conta__c = 'Ativa';
    newAccount.Type = 'Colaborador PJ';
    newAccount.Colaborador_RCS__c = true;
    newAccount.RecordTypeId = newRecordType[0].Id;
    insert newAccount;


    List<SFDC_Candidate__c> lstCandidato2 = new List<SFDC_Candidate__c>([
        SELECT Name
        FROM SFDC_Candidate__c
        WHERE Name = 'Test'
    ]);
    System.debug(lstCandidato2);

    lstCandidato2[0].Name = 'Teste';
    update lstCandidato2[0];

    List<Account> lstTeste = new List<Account>([
        SELECT Status_da_conta__c, FirstName
        FROM Account
        WHERE FirstName = 'Test'
    ]);
    System.debug(lstTeste);

    lstTeste[0].Status_da_conta__c = 'Inativa';
    update lstTeste[0];
    System.debug(lstTeste);

    delete newCandidato;
    delete newAccount;


}

}
1
  • 1
    You do not make a single assertion in this test. It has essentially no value even if it does increase your coverage metric. – Adrian Larson Aug 21 '15 at 17:35
1

It doesn't look to me as though you're testing for either of the possibilities for 'Genero'. With a method for each, you'd easily increase your coverage.

You need a method for when it's male and another method for when it's female. You need enough methods in your test class to cover all the If - Else If possibilities in your conditional statements. Each one requires a different test method.

Also, make certain you're populating all of the fields that your class uses like Origem_da_conta__c. If you don't, those lines won't be covered. You've not done that, thus why you don't have the coverage. Add debug statements nested within your If statements you can view in the debug logs to see if the code inside them is being executed.

You might find How do I start to debug my own Apex code? helpful to you.

2
  • Did it and got 73% now, I don't know if I'm doing something wrong... – Airton Melo Aug 21 '15 at 17:18
  • 1
    See edited answer – crmprogdev Aug 21 '15 at 17:44
1

enter image description here

Just to show what's happening, I don't think the test class is actually testing the class...

0

enter image description here

The class is not appearing in the sidebar, IDK why...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.