You need to sign in to do that
Don't have an account?
Vikas 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.
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.
Thanks
Amit Chaudhary
All Answers
Thanks
Amit Chaudhary
while when I'm checking challenge via trailhead still I'm getting below error.
I just deleted your trigger and class and re created. Issue fixed. Enjoy :)
@istest
public class TestRestrictContactByName {
public static testmethod void y(){
contact c1= new contact(lastname='INVALIDNAME');
insert c1;
}
}