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
Vikas MishraVikas Mishra 

facing problem while running unit test class for apex trigger "RestrictContactByName"

I'm facing problem while Unit test for Apex triggers.
Install a simple Apex trigger, write unit tests that achieves 100% code coverage for the trigger, and run your Apex tests.The Apex trigger to test is called 'RestrictContactByName', and the code is available here. Copy and paste this trigger into your Developer Edition via the Developer Console.
'RestrictContactByName' is a trigger which blocks inserts and updates to any contact with a last name of 'INVALIDNAME'.
The unit tests must be in a separate Apex class called 'TestRestrictContactByName'.
The unit tests must cover scenarios for all lines of code included in the Apex trigger, resulting in 100% code coverage.
Run your test class at least once (via the Developer Console) before attempting to verify this challenge.

from the developer console window I am getting class code coverage 100%, but when checking the challenge it showing the error as 
"Challenge not yet complete... here's what's wrong: 
The 'RestrictContactByName' class did not achieve 100% code coverage via your test methods"




test class I'm running is:-

@isTest
private class TestRestrictContactByName {

    static testMethod void  metodoTest() 
    {
    
        List<Contact> con= new List<Contact>();
        Contact c1 = new Contact(FirstName='Franc', LastName='Riggie' , email='Test@test.com');
        Contact c2 = new Contact(FirstName='Frank', LastName = 'INVALIDNAME',email='Test@test.com');
        con.add(c1);
        con.add(c2);
        
        Test.startTest();
            try
            {
                insert con;
            }
            catch(Exception ee)
            {
            }
        
        Test.stopTest(); 
        
    }
    
}

Kindly suggest.
 
Best Answer chosen by Vikas Mishra
Amit Chaudhary 8Amit Chaudhary 8
Please try below code I hope that will help you
@isTest
private class TestRestrictContactByName {

    static testMethod void  metodoTest() 
    {
    
        List<Contact> listContact= new List<Contact>();
        Contact c1 = new Contact(FirstName='Francesco', LastName='Riggio' , email='Test@test.com');
        Contact c2 = new Contact(FirstName='Francesco1', LastName = 'INVALIDNAME',email='Test@test.com');
        listContact.add(c1);
        listContact.add(c2);
        
        Test.startTest();
            try
            {
                insert listContact;
            }
            catch(Exception ee)
            {
            }
        
        Test.stopTest(); 
        
    }
    
}
Your Trigger should be like below
rigger RestrictContactByName on Contact (before insert, before update) {
    
    //check contacts prior to insert or update for invalid data
    For (Contact c : Trigger.New) {
        if(c.LastName == 'INVALIDNAME') {   //invalidname is invalid
            c.AddError('The Last Name "'+c.LastName+'" is not allowed for DML');
        }

    }



}
Please let us know if this will help you

Thanks
Amit Chaudhary


 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try below code I hope that will help you
@isTest
private class TestRestrictContactByName {

    static testMethod void  metodoTest() 
    {
    
        List<Contact> listContact= new List<Contact>();
        Contact c1 = new Contact(FirstName='Francesco', LastName='Riggio' , email='Test@test.com');
        Contact c2 = new Contact(FirstName='Francesco1', LastName = 'INVALIDNAME',email='Test@test.com');
        listContact.add(c1);
        listContact.add(c2);
        
        Test.startTest();
            try
            {
                insert listContact;
            }
            catch(Exception ee)
            {
            }
        
        Test.stopTest(); 
        
    }
    
}
Your Trigger should be like below
rigger RestrictContactByName on Contact (before insert, before update) {
    
    //check contacts prior to insert or update for invalid data
    For (Contact c : Trigger.New) {
        if(c.LastName == 'INVALIDNAME') {   //invalidname is invalid
            c.AddError('The Last Name "'+c.LastName+'" is not allowed for DML');
        }

    }



}
Please let us know if this will help you

Thanks
Amit Chaudhary


 
This was selected as the best answer
Vikas MishraVikas Mishra
in developer console while running this trigger, 100% code coverage I'm getting.

while when I'm checking challenge via trailhead still I'm getting below error.User-added image
Amit Chaudhary 8Amit Chaudhary 8
If possible can you please share your user Name and password on my email id (amit.salesforce21@gmail.com) so that i can check the issue.
Vikas MishraVikas Mishra
please check your mailbox.
Amit Chaudhary 8Amit Chaudhary 8
Hi Vikas,

I just deleted your trigger and class and re created. Issue fixed. Enjoy :)
vijay kumar kvijay kumar k
Try this once

@istest
public class TestRestrictContactByName {
    public static testmethod void y(){
        contact c1= new contact(lastname='INVALIDNAME');
        insert c1;
    }
}