You need to sign in to do that
Don't have an account?
Getting only 66% code coverage. what is wrong in my Test code.??
My Trigger is as following:-
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');
}
}
}
--------------------------------------------------
And my Test class as follows:-
@isTest
private class TestRestrictContactByName
{
@isTest static void TestContact()
{
Contact con=new Contact(FirstName='Arjun', LastName='Mahi');
if(con.LastName=='INVALIDNAME')
{
con.AddError('The Last Name "'+con.LastName+'" is not allowed for DML');
}else{insert con;}
Contact conn=new Contact(FirstName='Arjun', LastName='Kapur');
if(conn.LastName=='INVALIDNAME')
{
conn.AddError('The Last Name "'+conn.LastName+'" is not allowed for DML');
}else {update conn;}
Contact com=new Contact(FirstName='Rama', LastName='INVALIDNAME');
if(com.LastName=='INVALIDNAME')
{
com.AddError('The Last Name "'+com.LastName+'" is not allowed for DML');
}else {insert conn;}
}
}
plz help me complete this Challenge :)
Thanx in Advance :)
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');
}
}
}
--------------------------------------------------
And my Test class as follows:-
@isTest
private class TestRestrictContactByName
{
@isTest static void TestContact()
{
Contact con=new Contact(FirstName='Arjun', LastName='Mahi');
if(con.LastName=='INVALIDNAME')
{
con.AddError('The Last Name "'+con.LastName+'" is not allowed for DML');
}else{insert con;}
Contact conn=new Contact(FirstName='Arjun', LastName='Kapur');
if(conn.LastName=='INVALIDNAME')
{
conn.AddError('The Last Name "'+conn.LastName+'" is not allowed for DML');
}else {update conn;}
Contact com=new Contact(FirstName='Rama', LastName='INVALIDNAME');
if(com.LastName=='INVALIDNAME')
{
com.AddError('The Last Name "'+com.LastName+'" is not allowed for DML');
}else {insert conn;}
}
}
plz help me complete this Challenge :)
Thanx in Advance :)
Do keep this in mind and don't give out the code directly.
All Answers
All you have to do is to insert a contact whose LastName is INVALIDNAME. Then catch the exception (.addError throws it). Can't give you the code, but I can help you in understanding what I said.
--
Abhi
Try this
Regards,
Bhanu Mahesh
Do keep this in mind and don't give out the code directly.
Use Database.insert(con) to cover add error Lines in your Test Class...
Nice 1 Abhi.. :)
and what can i do for the Update .???
For this excercise you dont need to check for update, since there is conditional statement checking for "update" and "insert" (trigger.isInsert or trigger.isUpdate). BUT as a best practise, it's always good to cover these.
So simply insert a contact with any LastName other than INVALIDNAME, then update the contact and make the LastName=INVALIDNAME.
Check this out.
@istest
private class TestRestrictContactByName {
@istest static void testname(){
contact c = new contact(firstname='Satya',lastname='INVALIDNAME');
test.startTest();
database.SaveResult result = database.insert(c,false);
test.stopTest();
system.assertEquals('The Last Name "INVALIDNAME" is not allowed for DML', result.getErrors()[0].getMessage());
}
}
In your test case. You are creating a contact for the test case with last name 'Mahi'. So the 'if' condition would never have a last name of INVALIDNAME to be evaluated and I feel the those statements can be removed.
Contact con=new Contact(FirstName='Arjun', LastName='Mahi');
if(con.LastName=='INVALIDNAME')
{
con.AddError('The Last Name "'+con.LastName+'" is not allowed for DML');
}else{insert con;}
private class TestRestrictContactByName {
@isTest static void createcontact(){
// Test data setup contact with last name is invalid name
Test.startTest();
Contact con = new Contact(LastName ='INVALIDNAME');
Database.UpsertResult result = Database.upsert(con,false);
Test.stopTest();
// Verify
// In this case the contact creation should have been stopped by the trigger,
// so verify that we got back an error.
System.assert(!result.isSuccess());
System.assert(result.getErrors().size() > 0);
System.assertEquals('The Last Name "INVALIDNAME" is not allowed for DML.' ,result.getErrors()[0].getMessage());
}
}
Try This Code
-----------------------------------
@isTest
public class TestRestrictContactByName {
static testmethod void test(){
Contact con = New Contact(LastName = 'INVALIDNAME');
Test.startTest();
insert con;
Test.stopTest();
}
}
Thanks
Siri Chandana
@isTest
public class TestRestrictContactByName {
@isTest static void insertSuccess(){
boolean insertFailed;
Contact con = new Contact(
FirstName = 'John',
LastName = 'Travolta'
);
try{
Test.startTest();
insert(con);
Test.stopTest();
}catch(Exception e){
insertFailed = null;
}
system.assertEquals(null, insertFailed);
}
@isTest static void insertFailed(){
boolean insertFailed ;
Contact con = new Contact(
FirstName = 'John',
LastName = 'INVALIDNAME'
);
try{
Test.startTest() ;
insert(con);
Test.stopTest();
}catch(Exception e){
insertFailed = true;
}
system.assertEquals(true, insertFailed);
}
}