+ Start a Discussion
Anudeep BAnudeep B 

Test method not passed

I wrote a test class for this trigger, it is not passed what is the wrong in it?

Trigger:

trigger dupesincont on Lead (before insert, before update) {
    for(lead l: trigger.new)
    {
        if(l.email!=null)
        {
        list<contact> dupcont = [select id,email from contact where email =: l.email];
        string errormessage = 'Name already Exist with this email';
        errormessage += '  Record ID is ' +dupcont[0].id;
        l.addError(errormessage);
        }
    }
}



Test Class:

@isTest
public class trigg_dupesincont {
static testmethod void dupesfind(){

    string aderror;
    contact c = new contact();
    c.lastname = 'Jet Lee';
    c.email = 'Jets@gmail.com';
    insert c;
    

    
    lead l = new lead();
    l.lastname = 'Bruce';
    l.email = 'Jets@gmail.com';
    
        list<contact> cons = [select id,email from contact where email =:l.email];
        if(cons.size()<0)
        {
        system.assertequals(0,cons.size());
        insert l;
        }
        
        else
        {
        system.assertequals('Already in Contact', aderror);
        }
}
}
Amit Chaudhary 8Amit Chaudhary 8
Hi Anudeep B,

Please try below code :-
@isTest
public class trigg_dupesincont 
{
	static testmethod void dupesfind()
	{
		contact c = new contact();
		c.lastname = 'Jet Lee';
		c.email = 'Jets@gmail.com';
		insert c;
		
		lead l = new lead();
		l.lastname = 'Bruce';
		l.email = 'Jets@gmail.com';

		try
		{
			insert l;
		}
		catch(Exception ee)
		{
			// System.assert(e.getMessage().contains('Name already Exist with this email'));
		}
	}
}

Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help

 
JayantJayant

2 major issues, 1 each in business and test logic.

1. Business Logic - 
-------------------------------------------------------
 if(l.email!=null)
        {
        list<contact> dupcont = [select id,email from contact where email =: l.email];
        string errormessage = 'Name already Exist with this email';
        errormessage += '  Record ID is ' +dupcont[0].id;
        l.addError(errormessage);
        }
--------------------------------------------------------

errormessage += '  Record ID is ' +dupcont[0].id; - Here its being assumed that if the Lead has an email, it would be found in Contacts as well.
What you need is  - after the SOQL, check if dupcont actually has a Contact and only then append the error message. In current code, you will get an Index out of bounds exception whenever a Contact is not existing in the database with the same email as Lead being inserted or updated (because of 0th index used in dupcont[0].id).

Additionally, you also have the Contact SOQL inside a For loop, please bring it out to make it bulk safe.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2. Test Logic - 
---------------------------------------------------------
 list<contact> cons = [select id,email from contact where email =:l.email];
        if(cons.size()<0)
        {
        system.assertequals(0,cons.size());
        insert l;
        }
        
        else
        {
        system.assertequals('Already in Contact', aderror);
        }
-----------------------------------------------------------

if(cons.size()<0) should be if(cons.size()==0), list size will never be negative (or less than 0).

You don't need to fire the SOQL explicitly on Contact, just insert the Lead and trigger will run and conclude if its a duplicate.

Since you are adding an exception in trigger if its a duplicate, you should use Try...Catch... in the test method and once your trigger has added the exception to the record, your Catch block in test method would catch it. Else your test will fail as a result of runtime exception (added by the trigger).


Good Luck !