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
Lucas Arruda 8Lucas Arruda 8 

trigger test class document

Hello,

I'm trying to test the trigger below, however my test is not passing in the IF condition.

Trigger
trigger ValidateLead on Lead (before insert, before update){

	if (Trigger.isBefore) {
    	for(Lead led : trigger.new){

    		Lead leds = [SELECT Id, Name FROM Lead WHERE id=: led.Id];
    		
    			if(ValidaDocumento.isCNPJ(led.CNPJ__c))
    				led.CNPJ__c = ValidaDocumento.imprimeCNPJ(led.CNPJ__c);
    			else
    				led.addError('O CNPJ informado é Inválido');
	    }
    }
}

test class:
public static testMethod void createLead() {
	    
	    Lead prospect = new Lead(
	        
			LastName = 'TestProspect',
			CNPJ__c = 'teste',
			Company = 'TestCompany',
			Estado__c = 'SP'
		);
		ValidaDocumento.isCNPJ(prospect.CNPJ__c);
		ValidaDocumento.imprimeCNPJ(prospect.CNPJ__c);
		insert prospect;
		
		Lead prospect2 = new Lead(
	        
			LastName = 'TestProspect',
			CNPJ__c = '68119173000167',
			Company = 'TestCompany',
			Estado__c = 'SP'
		);
		ValidaDocumento.isCNPJ(prospect2.CNPJ__c);
		insert prospect2;
		
		Lead prospect3 = new Lead(
	        
			LastName = 'TestProspect',
			CNPJ__c = '00000000000000',
			Company = 'TestCompany',
			Estado__c = 'SP'
		);
		insert prospect3;
		ValidaDocumento.isCNPJ(prospect3.CNPJ__c);
		
		Lead prospect4 = new Lead(
	        
			LastName = 'TestProspect',
			CNPJ__c = '11111111111111',
			Company = 'TestCompany',
			Estado__c = 'SP'
		);
		insert prospect4;
		ValidaDocumento.isCNPJ(prospect4.CNPJ__c);
		
		Lead prospect5 = new Lead(
	        
			LastName = 'TestProspect',
			CNPJ__c = '681191730001',
			Company = 'TestCompany',
			Estado__c = 'SP'
		);
		insert prospect5;
	    ValidaDocumento.isCNPJ(prospect5.CNPJ__c);
	}
}
I'm in doubt how to make the test pass through my if conditional.
 
Best Answer chosen by Lucas Arruda 8
Maharajan CMaharajan C
Hi Lucas,

Remove the 6th line from trigger. In before insert you will not have id for the record. If you are refering the line 6 then code will throw the error on Lead Insert.

Trigger should be like below :

trigger ValidateLead on Lead (before insert, before update){
    if (Trigger.isBefore) {
        for(Lead led : trigger.new){            
                if(ValidaDocumento.isCNPJ(led.CNPJ__c))
                    led.CNPJ__c = ValidaDocumento.imprimeCNPJ(led.CNPJ__c);
                else
                    led.addError('O CNPJ informado é Inválido');
        }
    }
}

Below Test Class Should be fine but include all the Mandatory fields of lead record :

public static testMethod void createLead() {
        
        Lead prospect = new Lead(
            
            LastName = 'TestProspect',
            CNPJ__c = 'teste',
            Company = 'TestCompany',
            Estado__c = 'SP'
        );
        insert prospect;
        
        Lead prospect2 = new Lead(
            
            LastName = 'TestProspect',
            CNPJ__c = '68119173000167',
            Company = 'TestCompany',
            Estado__c = 'SP'
        );
        insert prospect2;
        
        Lead prospect3 = new Lead(
            
            LastName = 'TestProspect',
            CNPJ__c = '00000000000000',
            Company = 'TestCompany',
            Estado__c = 'SP'
        );
        insert prospect3;
        
        Lead prospect4 = new Lead(
            
            LastName = 'TestProspect',
            CNPJ__c = '11111111111111',
            Company = 'TestCompany',
            Estado__c = 'SP'
        );
        insert prospect4;
        
        Lead prospect5 = new Lead(
            
            LastName = 'TestProspect',
            CNPJ__c = '681191730001',
            Company = 'TestCompany',
            Estado__c = 'SP'
        );
        insert prospect5;
    }
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Lucas,

Remove the 6th line from trigger. In before insert you will not have id for the record. If you are refering the line 6 then code will throw the error on Lead Insert.

Trigger should be like below :

trigger ValidateLead on Lead (before insert, before update){
    if (Trigger.isBefore) {
        for(Lead led : trigger.new){            
                if(ValidaDocumento.isCNPJ(led.CNPJ__c))
                    led.CNPJ__c = ValidaDocumento.imprimeCNPJ(led.CNPJ__c);
                else
                    led.addError('O CNPJ informado é Inválido');
        }
    }
}

Below Test Class Should be fine but include all the Mandatory fields of lead record :

public static testMethod void createLead() {
        
        Lead prospect = new Lead(
            
            LastName = 'TestProspect',
            CNPJ__c = 'teste',
            Company = 'TestCompany',
            Estado__c = 'SP'
        );
        insert prospect;
        
        Lead prospect2 = new Lead(
            
            LastName = 'TestProspect',
            CNPJ__c = '68119173000167',
            Company = 'TestCompany',
            Estado__c = 'SP'
        );
        insert prospect2;
        
        Lead prospect3 = new Lead(
            
            LastName = 'TestProspect',
            CNPJ__c = '00000000000000',
            Company = 'TestCompany',
            Estado__c = 'SP'
        );
        insert prospect3;
        
        Lead prospect4 = new Lead(
            
            LastName = 'TestProspect',
            CNPJ__c = '11111111111111',
            Company = 'TestCompany',
            Estado__c = 'SP'
        );
        insert prospect4;
        
        Lead prospect5 = new Lead(
            
            LastName = 'TestProspect',
            CNPJ__c = '681191730001',
            Company = 'TestCompany',
            Estado__c = 'SP'
        );
        insert prospect5;
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
Lucas Arruda 8Lucas Arruda 8
Thank you  Maharajan C!

After I removed the query the test code worked!

Regards,
Lucas