You need to sign in to do that
Don't have an account?
venkyyy
Cant get code coverage 100% for my trigger,(but my test class was passed succuesfully)
Hi all,
I have created a trigger for my scenario and i wrote test class for that as i copied bellow and please help me out what mistake i did in test class,
Trigger is:
----------------
trigger DelearNotToUpdateCase on Case (before Update) {
Profile ProfileName = [select Name from profile where id = :userinfo.getProfileId()];
if(ProfileName.Name =='vinod_cs' && UserUtil.CurrentUser.ContactId == Null){
if (trigger.isupdate){
for( Case cnew : Trigger.new){
Try{
if(cnew.CreatedDate != Null && cnew.prasads__no_of_comments__c == Trigger.oldMap.get(cnew.id).prasads__no_of_comments__c && cnew.prasads__apex_update__c == Trigger.oldMap.get(cnew.id).prasads__apex_update__c ){
system.debug('>>>>>>>You may not edit a case once it has been created. Please create a new case comment at the bottom of the page to request your changes.<<<<<<<<<<');
Trigger.newMap.get((Id)cnew.get('Id')).addError('You may not edit a case once it has been created. Please create a new case comment at the bottom of the page to request your changes.');
}
}Catch(exception e){ }
}
}
}
}
------------------------
Test class is :
@isTest
public class DelearNotToUpdateCaseTest {
Static testmethod void NotupdateTest(){
DelearNotToUpdateCase scc = new DelearNotToUpdateCase();
Profile ProfileName=new Profile();
ProfileName.Name='vinod_cs';
profilename=[Select name from profile where name='vinod_cs'];
system.assertEquals(profileName.Name,'vinod_cs');
list<Case> ca=new list<case>();
ca=[Select id,createdDate, status,origin from case];
case c=new case();
c.status='New';
c.Origin='Phone';
c.no_of_comments__c= 12;
c.apex_update__c=11;
// c.CreatedDate= System.today();
ca.add(c);
insert ca;
system.assertEquals(c.Status,'New');
System.assertEquals(c.Origin,'Phone');
casecomment cm=new casecomment();
cm.IsPublished=false;
cm.CommentBody='Test';
cm.ParentId=c.id;
insert cm;
System.assertEquals(cm.IsPublished,False);
system.assertEquals(cm.CommentBody,'Test');
system.assertequals(cm.ParentId,c.Id);
}
}
I have created a trigger for my scenario and i wrote test class for that as i copied bellow and please help me out what mistake i did in test class,
Trigger is:
----------------
trigger DelearNotToUpdateCase on Case (before Update) {
Profile ProfileName = [select Name from profile where id = :userinfo.getProfileId()];
if(ProfileName.Name =='vinod_cs' && UserUtil.CurrentUser.ContactId == Null){
if (trigger.isupdate){
for( Case cnew : Trigger.new){
Try{
if(cnew.CreatedDate != Null && cnew.prasads__no_of_comments__c == Trigger.oldMap.get(cnew.id).prasads__no_of_comments__c && cnew.prasads__apex_update__c == Trigger.oldMap.get(cnew.id).prasads__apex_update__c ){
system.debug('>>>>>>>You may not edit a case once it has been created. Please create a new case comment at the bottom of the page to request your changes.<<<<<<<<<<');
Trigger.newMap.get((Id)cnew.get('Id')).addError('You may not edit a case once it has been created. Please create a new case comment at the bottom of the page to request your changes.');
}
}Catch(exception e){ }
}
}
}
}
------------------------
Test class is :
@isTest
public class DelearNotToUpdateCaseTest {
Static testmethod void NotupdateTest(){
DelearNotToUpdateCase scc = new DelearNotToUpdateCase();
Profile ProfileName=new Profile();
ProfileName.Name='vinod_cs';
profilename=[Select name from profile where name='vinod_cs'];
system.assertEquals(profileName.Name,'vinod_cs');
list<Case> ca=new list<case>();
ca=[Select id,createdDate, status,origin from case];
case c=new case();
c.status='New';
c.Origin='Phone';
c.no_of_comments__c= 12;
c.apex_update__c=11;
// c.CreatedDate= System.today();
ca.add(c);
insert ca;
system.assertEquals(c.Status,'New');
System.assertEquals(c.Origin,'Phone');
casecomment cm=new casecomment();
cm.IsPublished=false;
cm.CommentBody='Test';
cm.ParentId=c.id;
insert cm;
System.assertEquals(cm.IsPublished,False);
system.assertEquals(cm.CommentBody,'Test');
system.assertequals(cm.ParentId,c.Id);
}
}
In our trigger you have checked that oldmap values are equal to new map. Change that condition to not equal to like: Now test for it would be: Now coverage would increase.
Thanks,
Neetu
All Answers
You have written trigger on before update of Case and in test class you are only inserting the case, you need to update the case to run the trigger code,
Let me know if I help you more and if this post helps, mark it as best answer.
Thanks,
Neetu
Thanks for advice,,
i modified the trigger with update statement insted of insert as u guide, but i am getting bellow error.
I guess in the test class you have updated the insert with update statement like:
nsert ca; is now update ca;
But this will not resolve the issue because you cannot update any record without inserting it. Follow these steps:
1. Insert case like insert ca
2. After inserting case comment, update the case like update ca;
This will execute the trigger. If you still face the issue, please paste your update code too.
Thanks,
Neetu
Done for 25% code coverage now. bellow is my modified code and i was struck again to get 100% coverage, guide me to achive it.
Thanks,
Venkyy.
public class DelearNotToUpdateCaseTest {
Static testmethod void NotupdateTest(){
// Profile creation.
Profile ProfileName=new Profile();
ProfileName.Name='vinod_cs';
profilename=[Select name from profile where name='vinod_cs'];
system.assertEquals(profileName.Name,'vinod_cs');
//User Creration
User u = new User(Alias = 'Raaaja', Email='raajayekkala@testorg.com',
EmailEncodingKey='UTF-8', LastName='Yekkala', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = ProfileName.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='raajayekkala@testorg.com');
System.runAs(u) {
// The following code runs as user 'u'
System.debug('Current User: ' + UserInfo.getUserName());
System.debug('Current Profile: ' + UserInfo.getProfileId());
}
list<Case> ca=new list<case>();
ca=[Select id,createdDate, status,origin from case];
case c=new case();
c.status='New';
c.Origin='Phone';
c.no_of_comments__c= 12;
c.apex_update__c=11;
ca.add(c);
insert ca;
c.status='New1';
c.Origin='Phone1';
c.no_of_comments__c= 123;
c.apex_update__c=111;
update ca;
system.assertEquals(c.Status,'New1');
System.assertEquals(c.Origin,'Phone1');
casecomment cm=new casecomment();
cm.IsPublished=false;
cm.CommentBody='Test';
cm.ParentId=c.id;
insert cm;
cm.IsPublished=false;
cm.CommentBody='Test';
update cm;
System.assertEquals(cm.IsPublished,False);
system.assertEquals(cm.CommentBody,'Test');
system.assertequals(cm.ParentId,c.Id);
}
}
In our trigger you have checked that oldmap values are equal to new map. Change that condition to not equal to like: Now test for it would be: Now coverage would increase.
Thanks,
Neetu