+ Start a Discussion
Hung Vo 891Hung Vo 891 

The 'RestrictContactByName' class did not achieve 100% code coverage via your test methods

I'm doing the "Test Apex Triggers" challenge and get this error:
"The 'RestrictContactByName' class did not achieve 100% code coverage via your test methods"

I also attach my code here, can you show me where could it be the wrong spot?
 
@isTest
public class TestRestrictContactByName {
    @isTest static void TestInsertContact_INVALIDNAME(){
		Contact contact = new Contact(LastName='INVALIDNAME');
        Test.startTest();
        Database.SaveResult result = Database.insert(contact);
        Test.stopTest();
        System.assert(!result.isSuccess());
        System.assert(result.getErrors().size() > 0);
        System.assertEquals('The Last Name "'+contact.LastName+'" is not allowed for DML',
                             result.getErrors()[0].getMessage());
    }
    
    @isTest static void TestInsertContact_VALIDNAME(){
		Contact contact = new Contact(LastName='VALIDNAME');
        Test.startTest();
        Database.SaveResult result = Database.insert(contact);
        Test.stopTest();
        System.assert(result.isSuccess());
    }
    
    @isTest(SeeAllData=true) static void TestUpdateContact_INVALIDNAME(){
		Contact contact = [SELECT Id FROM Contact WHERE LastName = 'Test Contact' LIMIT 1];
        contact.LastName = 'INVALIDNAME';
        Test.startTest();
        Database.SaveResult result = Database.update(contact);
        Test.stopTest();
        System.assert(!result.isSuccess());
        System.assert(result.getErrors().size() > 0);
        System.assertEquals('The Last Name "'+contact.LastName+'" is not allowed for DML',
                             result.getErrors()[0].getMessage());
    }
    
    @isTest(SeeAllData=true) static void TestUpdateContact_VALIDNAME(){
		Contact contact = [SELECT Id FROM Contact WHERE LastName = 'Test Contact' LIMIT 1];
        contact.LastName = 'VALIDNAME';
        Test.startTest();
        Database.SaveResult result = Database.update(contact);
        Test.stopTest();
        System.assert(result.isSuccess());
    }
}

 
Best Answer chosen by Hung Vo 891
Hung Vo 891Hung Vo 891
Hello,

After a few try out, my code worked.
The reason maybe something went wrong and I just log out and login again, and everything works fine.
Also remember to to this step
Run your test class at least once (via 'Run All' tests the Developer Console) before attempting to verify this challenge.

 

All Answers

Shanu Kumar 18Shanu Kumar 18
Hi Hung Vo
Please use the following code. It will give you 100% code coverage.
trigger 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');
		}
	}
}

TestRestrictContactByName.apxc
@isTest
private class TestRestrictContactByName {
    @isTest static void TestContact(){
        Contact con = New Contact(LastName='INVALIDNAME');
        insert con;
    }

}

Thanks,
Ajay K DubediAjay K Dubedi
Hi Hung,

Try the codes below:

Trigger:
 
trigger 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');
        }

    }

}

Class:
 
@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(); 
        
    }
    
}

Note: After saving these two please go to developer console --> test --> run all (if you are using anything else please ensure this is done). This will solve your problem.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Hung Vo 891Hung Vo 891
Hello,

After a few try out, my code worked.
The reason maybe something went wrong and I just log out and login again, and everything works fine.
Also remember to to this step
Run your test class at least once (via 'Run All' tests the Developer Console) before attempting to verify this challenge.

 
This was selected as the best answer