+ Start a Discussion
Hitesh Khatri 3Hitesh Khatri 3 

What's the code for "Unit Test for simple Appex Trigger" Challange?

I have written the Code for Test Class"TestRestrictContactByName'.
@isTest
private class TestRestrictContactByName {
    @isTest static void testRestrictContacts(){
        Contact con1 = new Contact(FirstName='Htesh', LastName='khatri');
        Test.startTest();
        Database.SaveResult Result = Database.insert(con1,false);
        Test.stopTest();
        System.assert(!result.isSuccess());
        System.assert(result.getErrors().size()>0);
        System.assertEquals('The Last Name "'+con1.LastName+'" is not allowed for DML',result.getErrors()[0].getMessage());
 }
    After Running this test i am getting an error 'Challenge not yet complete... here's what's wrong:
The 'RestrictContactByName' class did not achieve 100% code coverage via your test methods' .

Please help me in solving this.
Best Answer chosen by Hitesh Khatri 3
Vinoth Vijaya BaskerVinoth Vijaya Basker
Hi Hitesh, 

Please find the below test class, 
 
@isTest
private class TestRestrictContactByName{
    
    @isTest static void testOne() {
        List<Contact> listOfContacts = new List<Contact>();
        for(integer i = 0; i < 200; i++){
            Contact c = new Contact(FirstName = 'FNAME', LastName = 'INVALIDNAME');
            listOfContacts.add(c);
        }
        try{
            insert listOfContacts;
        }
        catch(DMLException ex){
            for(integer i = 0; i < 200; i++){
                System.assert(ex.getMessage().contains('The Last Name "'+listOfContacts[i].LastName+'" is not allowed for DML'));
            }
        }        
    }    
}

Thanks,
Vinoth

All Answers

Vinoth Vijaya BaskerVinoth Vijaya Basker
Hi Hitesh, 

Please find the below test class, 
 
@isTest
private class TestRestrictContactByName{
    
    @isTest static void testOne() {
        List<Contact> listOfContacts = new List<Contact>();
        for(integer i = 0; i < 200; i++){
            Contact c = new Contact(FirstName = 'FNAME', LastName = 'INVALIDNAME');
            listOfContacts.add(c);
        }
        try{
            insert listOfContacts;
        }
        catch(DMLException ex){
            for(integer i = 0; i < 200; i++){
                System.assert(ex.getMessage().contains('The Last Name "'+listOfContacts[i].LastName+'" is not allowed for DML'));
            }
        }        
    }    
}

Thanks,
Vinoth
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class. That worked for me
@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(); 
        
    }
    
}
Please let us know if that will help you

Thanks
Amit Chaudhary