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
MNaethlerMNaethler 

Code Coverage for nested If's

Hey guys, i got a code coverage from 89%.

 

Well i want to max it to 100%, just for myself, but i dont know how to to improve the code coverage for nested if statements, can anyone give me an idea?

 

see code below 

the underlined is not covered, which i want to get covered

 

trigger : 

 

trigger preventDuplicateLeadEntriesByEmailAndPhoneAndMobilePhone on Lead (before insert, before update){
    Map<String, Lead> leadMap = new Map<String, Lead>();
    for (Lead lead : System.Trigger.new){
        // To make sure we don't treat an email address, phone or mobilephone number 
        // that isn't changing during an update as a duplicate.   
        if ((lead.Email != null) && (System.Trigger.isInsert || (lead.Email != System.Trigger.oldMap.get(lead.Id).Email))){
            if (leadMap.containsKey(lead.Email)){
                lead.Email.addError('Another lead has the same email address.');            } 
            else{
                leadMap.put(lead.Email, lead);
            }
        }  
        if ((lead.Phone != null) && (System.Trigger.isInsert || (lead.Phone != System.Trigger.oldMap.get(lead.Id).Phone))){
            if (leadMap.containsKey(lead.Phone)){
                lead.Phone.addError('Another lead has the same Phone number.');            } 
            else{
                leadMap.put(lead.Phone, lead);
            }
        }
        if ((lead.Mobilephone__c != null) && (System.Trigger.isInsert || (lead.Mobilephone__c != System.Trigger.oldMap.get(lead.Id).Mobilephone__c))){
            if (leadMap.containsKey(lead.Mobilephone__c)){
                lead.Mobilephone__c.addError('Another lead has the same Mobilephone number.');            } 
            else{
                leadMap.put(lead.Mobilephone__c, lead);
            }
        }
        
        //To make sure that the customer has entered the streetnumber
        if (lead.Streetnumber__c == null){
        	lead.Streetnumber__c.addError('Please review your streetadress and paste your streetnumber here');
        }
        
        /*
        //To make sure that the Street and streetnumber isnt äquivalent to an existing phone or mobilephone number  
        */
                
        // To make sure that the customer has entered a mobilephone or telephon number or a address to contact him later
        if (lead.LastName == null && lead.Street == null && lead.Mobilephone__c == null && lead.Phone == null && lead.Email == null){
            lead.Mobilephone__c.addError('You need at least one contact option, a streetaddress, a phone or a mobilephone number');
        } 
    }
    
    // Using a single database query, find all the leads in the database that have the same 
    // email adress, phone number or mobilephone number as any of the leads being inserted or updated. 
    for (Lead lead : [SELECT Email FROM Lead WHERE Email IN :leadMap.KeySet()]){
        Lead newLead = leadMap.get(lead.Email);
        newLead.Email.addError('A lead with this email address already exists.');
    }
    for (Lead lead : [SELECT Phone FROM Lead WHERE Phone IN :leadMap.KeySet()]){
        Lead newLead = leadMap.get(lead.Phone);
        newLead.Phone.addError('A lead with this phone number already exists.');
    }
    for (Lead lead : [SELECT Mobilephone__c FROM Lead WHERE Mobilephone__c IN :leadMap.KeySet()]){
        Lead newLead = leadMap.get(lead.Mobilephone__c);
        newLead.Mobilephone__c.addError('A lead with this mobilephone number already exists.');
    }
}

 

testclass:

/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class TESTpreventDuplicatedLeadEntriesClass{		
	static Lead createLead(){
    	Lead lead = new Lead();        
        lead.LastName = 'Test';
        lead.Company = 'TestCompany';
        lead.Street = 'Teststrasse';
        lead.Streetnumber__c = '1';
        lead.Phone = '03412222531';
        lead.Mobilephone__c = '01777777771';
        lead.Email = 'test@gmail.com';
        return lead;
    }
    
	//Insertcontrol
	    static testMethod void shouldThrowExceptionOnInsertALeadWithADuplicateMobilePhoneNumber(){	
	        Lead leadA = createLead();
	        Lead leadB = createLead();
	        
		    insert leadA;
		    leadB.Phone = '03412222532';
			leadB.Email = 'test2@gmail.com';
		    try{
		    	insert leadB;
	    	}
		    catch(System.DmlException e){
		    	try {
		    		leadB.Mobilephone__c = '01777777772';
		    	}
		    	catch(System.DmlException e1){
		    		System.assertEquals('Error: ', e1.getDmlMessage(0));
		    	}
		    }
	    }
	    static testMethod void shouldThrowExceptionOnInsertALeadWithADuplicatePhoneNumber(){	
	        Lead leadA = createLead();
	        Lead leadB = createLead();
	        
	        insert leadA;
        	leadB.Mobilephone__c = '01777777772';
			leadB.Email = 'test2@gmail.com';
		    try{
		    	insert leadB;
	    	}
		    catch(System.DmlException e){
		    	try{
		    		leadB.Phone = '03412222532';
		    	}
		    	catch(System.DmlException e1){
		    		System.assertEquals('Error: ', e1.getDmlMessage(0));
		    	}
		    }
	    }
	    static testMethod void shouldThrowExceptionOnInsertALeadWithADuplicateEMailadress(){	
	        Lead leadA = createLead();
	        Lead leadB = createLead();
	        
	        insert leadA;
	        leadB.Phone = '03412222532';
        	leadB.Mobilephone__c = '01777777772';
		    try{
		    	insert leadB;
	    	}
		    catch(System.DmlException e){
		    	try{
		    		leadB.Email = 'test2@gmail.com';
		    	}
		    	catch(System.DmlException e1){
		    		System.assertEquals('Error: ', e1.getDmlMessage(0));
		    	}
		    }
	    }
	    static testMethod void shouldThrowExceptionOnInsertALeadWithoutAStreetnumber(){	
	        Lead leadA = createLead();
	        Lead leadB = createLead();
	        		        
		    insert leadA;
		    leadB.Phone = '03412222532';
        	leadB.Mobilephone__c = '01777777772';
			leadB.Email = 'test2@gmail.com';
			leadB.Streetnumber__c = '';
		    try{
		    	insert leadB;		    	
	    	}
		    catch(System.DmlException e){
		    	try{
		    		leadB.Streetnumber__c = '2';
		    	}
		    	catch(System.DmlException e1){
		    		System.assertEquals('Error: ', e1.getDmlMessage(0));
		    	}
		    }
	    }
	    static testMethod void shouldThrowExceptionOnInsertALeadWithoutAnyContactInformation(){	
	        Lead leadA = createLead();
	        Lead leadB = createLead();
	        
	        insert leadA;
	        leadB.Company = 'TestCompany';
	        leadB.Street = '';
	        leadB.Streetnumber__c = '';
	        leadB.Phone = '';
	        leadB.Mobilephone__c = '';
	        leadB.Email = '';
		    try{
		    	insert leadB;
	    	}
		    catch(System.DmlException e){
		    	try{
		    		leadB.Company = 'TestCompany';
					leadB.Street = 'advadf';
					leadB.Streetnumber__c = '2';
					leadB.Phone = '03412222532';
					leadB.Mobilephone__c = '01777777772';
					leadB.Email = 'test2@gmail.com';
		    	}
		    	catch(System.DmlException e1){
		    		System.assertEquals('Error: ', e.getDmlMessage(0));
		    	}
		    }
	    }
    //Insertcontrol end
    
    //Updatecontrol
    	static testMethod void shouldThrowExceptionOnUpdateALeadWithADuplicateMobilePhoneNumber(){	
	        Lead leadA = createLead();
	        Lead leadB = createLead();
	        
		    insert leadA;
		    leadB.Phone = '03412222532';
			leadB.Email = 'test2@gmail.com';
			leadB.Mobilephone__c = '01777777772';
			insert leadB;
		    try{
		    	leadB.Mobilephone__c = '01777777771';
		    	update leadB;
	    	}
		    catch(System.DmlException e){
		    	try {
		    		leadB.Mobilephone__c = '01777777772';
		    	}
		    	catch(System.DmlException e1){
		    		System.assertEquals('Error: ', e1.getDmlMessage(0));
		    	}
		    }
	    }
	    static testMethod void shouldThrowExceptionOnUpdateALeadWithADuplicatePhoneNumber(){	
	        Lead leadA = createLead();
	        Lead leadB = createLead();
	        
		    insert leadA;
		    leadB.Phone = '03412222532';
			leadB.Email = 'test2@gmail.com';
			leadB.Mobilephone__c = '01777777772';
			insert leadB;
		    try{
		    	leadB.Phone = '03412222531';
		    	update leadB;
	    	}
		    catch(System.DmlException e){
		    	try {
		    		leadB.Phone = '03412222532';
		    	}
		    	catch(System.DmlException e1){
		    		System.assertEquals('Error: ', e1.getDmlMessage(0));
		    	}
		    }
	    }
    	static testMethod void shouldThrowExceptionOnUpdateALeadWithADuplicateEMailadress(){	
	        Lead leadA = createLead();
	        Lead leadB = createLead();
	        
		    insert leadA;
		    leadB.Phone = '03412222532';
			leadB.Email = 'test2@gmail.com';
			leadB.Mobilephone__c = '01777777772';
			insert leadB;
		    try{
		    	leadB.Email = 'test@gmail.com';
		    	update leadB;
	    	}
		    catch(System.DmlException e){
		    	try {
		    		leadB.Email = 'test2@gmail.com';
		    	}
		    	catch(System.DmlException e1){
		    		System.assertEquals('Error: ', e1.getDmlMessage(0));
		    	}
		    }
	    }  
	    static testMethod void shouldThrowExceptionOnUpdateALeadWithoutAStreetnumber(){	
	        Lead leadA = createLead();
	        Lead leadB = createLead();
	        
		    insert leadA;
		    leadB.Phone = '03412222532';
			leadB.Email = 'test2@gmail.com';
			leadB.Mobilephone__c = '01777777772';
			leadB.Streetnumber__c = '2';
			insert leadB;
		    try{
		    	leadB.Streetnumber__c = '0';
		    	update leadB;
	    	}
		    catch(System.DmlException e){
		    	try {
		    		leadB.Streetnumber__c = '2';
		    	}
		    	catch(System.DmlException e1){
		    		System.assertEquals('Error: ', e1.getDmlMessage(0));
		    	}
		    }
	    }
	    static testMethod void shouldThrowExceptionOnUpdateALeadWithoutAnyContactInformation(){	
	        Lead leadA = createLead();
	        Lead leadB = createLead();
	        
		    insert leadA;
		    leadB.Phone = '03412222532';
			leadB.Email = 'test2@gmail.com';
			leadB.Mobilephone__c = '01777777772';
			leadB.Streetnumber__c = '2';
			insert leadB;
		    try{
		    	leadB.Company = 'TestCompany';
		        leadB.Street = '';
		        leadB.Streetnumber__c = '';
		        leadB.Phone = '';
		        leadB.Mobilephone__c = '';
		        leadB.Email = '';
		    	update leadB;
	    	}
		    catch(System.DmlException e){
		    	try {
		    		leadB.Company = 'TestCompany2';
					leadB.Street = 'Test2street';
					leadB.Streetnumber__c = '2';
					leadB.Phone = '03412222532';
					leadB.Mobilephone__c = '01777777772';
					leadB.Email = 'test2@gmail.com';
		    	}
		    	catch(System.DmlException e1){
		    		System.assertEquals('Error: ', e1.getDmlMessage(0));
		    	}
		    }
	    }
    //Updatecontrol end   
}

 

GunnarGunnar

In your test data, create new lead records with duplicate phone and email values.

 

I assume you are creating your test data in the test, and, that the test class is in a separate file?

 

If your validation rules don't allow duplicate phone or email values in the leads table, that test code will never fire, might as well remove it.

MNaethlerMNaethler

well, all my testmethods are firing, thats not the point which i've called about help.

 

testmethods

 

and sure the testclass is in a seperate file

 

i create 2 records based on 1 static leadcreate as u can see in the code. i just changes some values like in the first testcase : shouldThrowExceptionOnInsertALeadWithADuplicateMobilePhoneNumber

 

 static testMethod void shouldThrowExceptionOnInsertALeadWithADuplicateMobilePhoneNumber(){	
	        Lead leadA = createLead();
	        Lead leadB = createLead();
	        
		    insert leadA;
		    leadB.Phone = '03412222532';
        	leadB.Mobilephone__c = '01777777771';
			leadB.Email = 'test2@gmail.com';
		    try{
		    	insert leadB;
	    	}
		    catch(System.DmlException e){
		    	System.assertEquals('Error: ', e.getDmlMessage(0));
		    }
	    }

 


the normal values in the case of the check from mobilphone duplicates is : 0177111111, as u can seen too in my static lead create. i just have to change the email adress and phonenumber that he will give me the right dml error message(duplicate mobilephone number). If i wouldnt do that, he will give me the first eroor message at all, the email duplicate

 

 

for (Lead lead : [SELECT Email FROM Lead WHERE Email IN :leadMap.KeySet()]){
        Lead newLead = leadMap.get(lead.Email);
        newLead.Email.addError('A lead with this email address already exists.');
    }

 

so may u can lokk better through my code :)

 

but anyway i didnt know how to check in a testcase a nested if like this 

 

if ((lead.Email != null) && (System.Trigger.isInsert || (lead.Email != System.Trigger.oldMap.get(lead.Id).Email))){
            if (leadMap.containsKey(lead.Email)){
                lead.Email.addError('Another lead has the same email address.');
            } 
            else{
                leadMap.put(lead.Email, lead);
            }
        }

 

how can i reach the underlined error message? did i need the leadmap to get that?