You need to sign in to do that
Don't have an account?
prai
Test Class code coverage not increasing
Hi All,
i have wrote the trigger and now i am writting test class for this but code coverage is going only 67%, Please help me to increase the code coverage:
Here is the trigger:
trigger deleteandedittask on Task (before delete, before update) {
string rtypeid = [Select id,Name from RecordType where sObjectType='Task' and isActive=true and Name = 'FA-CRM System Task'].id;
set<Id> bypassProfileIdSet = new Set<id>();
bypassProfileIdSet.add(RoleProfile__c.getInstance('API Users').ID__c);
bypassProfileIdSet.add(RoleProfile__c.getInstance('System Administrator').ID__c);
if(trigger.isUpdate)
{
if(!bypassProfileIdSet.contains(userinfo.getprofileid()))
{
for (task t : Trigger.New)
{
String strwhatId = ''; String strwhoId = '';
If(t.whatid != null)strwhatid = String.valueOf(t.Whatid).substring(0 , 3);
If(t.whoid != null) strwhoid = String.valueOf(t.Whoid).substring(0 , 3);
if(strwhatid == '001' || strwhoid== '003' && t.whatid==null )
{
if( Trigger.oldMap.get(t.id).Status =='Completed')
{
if(t.RecordTypeId == rtypeid)
{
if(Trigger.oldMap.get(t.id).Status != Trigger.newmap.get(t.id).Status)
t.Status.addError('Cannot edit or delete Activity History.');
if(Trigger.oldMap.get(t.id).priority != Trigger.newmap.get(t.id).priority)
t.Priority.addError('Cannot edit or delete Activity History.');
if(Trigger.oldMap.get(t.id).Subject != Trigger.newmap.get(t.id).subject)
t.Subject.addError('Cannot edit or delete Activity History.');
if(Trigger.oldMap.get(t.id).Ownerid != Trigger.newmap.get(t.id).Ownerid)
t.Ownerid.addError('Cannot edit or delete Activity History.');
else
t.addError('Cannot edit or delete Activity History');
}
else
t.addError('Cannot edit or delete Activity History');
}
}
}
}
}
if(Trigger.IsDelete)
{
if(!bypassProfileIdSet.contains(userinfo.getprofileid()))
{
for (task t : Trigger.old)
{
String strwhatId = ''; String strwhoId = '';
If(t.whatid != null)strwhatid = String.valueOf(t.Whatid).substring(0 , 3);
If(t.whoid != null) strwhoid = String.valueOf(t.Whoid).substring(0 , 3);
if(strwhatid == '001' || (strwhoid== '003' && t.Whatid==null))
{
if(( Trigger.oldMap.get(t.id).Status =='Completed') ) {
t.addError('Cannot edit or delete Activity History.');
}
}
}
}
}
}
And here is my test class:
private class Testdeleteandedittask {
static testMethod void myUnitTest() {
//Init and Creating data
List<Profile> ProfileList=new List<Profile>();
String Userrecord='FA-CRM Platform';
Profile pro = [SELECT Id FROM profile WHERE name =:Userrecord];
User u1 = new User(alias = 'TaskF', email='newTaskuser@testorg.com', emailencodingkey='UTF-8', lastname='TaskFormat', languagelocalekey='en_US',localesidkey='en_US', profileid = pro.Id,timezonesidkey='America/Los_Angeles', username='newTaskFormat1@testorg.com',LR_Firm_ID__c=2);
insert u1;
Test.startTest();
System.Runas(u1){
String rectype=[Select id,Name from RecordType where sObjectType='Task' and isActive=true and Name = 'Advisor Tasks'].id;
Map<String,RecordType> RecortypeMap=new Map<String,RecordType>();
for(RecordType r:[select id,name from RecordType Where SobjectType = 'Account' and name='Person'])
{
RecortypeMap.put(r.name,r);
}
List<Account> AccountList=new List<Account>();
Account acc1 = new Account(name='account1',Mailing_Line1__c='Line1',PrimaryPhoneType__c='Business',PrimaryPhone__c='5423534', PrimaryPhoneFormat__c='TEST', SecondaryPhoneFormat__c='TEST');
AccountList.add(acc1);
Account acc2 = new Account(name='account2',Mailing_Line1__c='Line2',PrimaryPhoneType__c='Business',PrimaryPhone__c='5423534', PrimaryPhoneFormat__c='TEST', SecondaryPhoneFormat__c='TEST');
AccountList.add(acc2);
insert AccountList;
Profile[] Pr = [Select id from PRofile limit 2];
RoleProfile__c obj1 = new RoleProfile__c();
obj1.Name = 'API Users';
obj1.ID__c = PR[0].id;
obj1.Type__c = 'Profile';
Insert obj1;
RoleProfile__c obj2 = new RoleProfile__c();
obj2.Name = 'System Administrator';
obj2.ID__c = PR[1].id;
obj2.Type__c = 'Profile';
Insert obj2;
List<Task> tasklist = new List<Task>();
for(integer i=0;i<5;i++){
Task task = new Task();
task.Status='In Progress';
task.Priority='Normal';
task.whatid=AccountList[1].id;
task.subject='sw test'+i;
task.Comments__c='Completedd';
task.Description='compl';
tasklist.add(task);
}
Task task1 = new Task();
task1.Status='In Progress';
task1.Priority='Normal';
task1.whatid=AccountList[0].id;
task1.subject='sw test'+7;
task1.Comments__c='Completedd';
task1.Description='compl';
insert task1;
Task task2 = new Task();
task2.Status='In Progress';
task2.Priority='Normal';
task2.whatid=AccountList[0].id;
task2.subject='sw test'+8;
task2.Comments__c='Completedd';
task2.Description='compl';
insert task2;
task2.Priority='High';
task2.subject='sw test'+9;
update task2;
// tasklist.add(task);
insert tasklist;
System.assert(tasklist.size()>0);
List<Event> Eventlist = new List<Event>();
for(integer i=0;i<5;i++){
Event event = new Event();
event.EndDateTime = System.Now()+10;
event.StartDateTime = System.Now()+10;
event.whatid=AccountList[0].id;
event.subject='ev test'+i;
Eventlist.add(event);
}
insert eventlist;
Event event2 = new Event();
event2.EndDateTime = System.Now()+10;
event2.StartDateTime = System.Now()+10;
event2.whatid=AccountList[0].id;
event2.subject='ev test'+7;
insert event2;
System.assert(tasklist.size()>0);
List<Event> NewEventlist = new List<Event>();
for(Event ev23:eventlist){
ev23.Event_Status__c='Completed';
ev23.Comments__c='Closed in Update ';
NewEventlist.add(ev23);
}
update NewEventlist;
System.assert(NewEventlist.size()>0);
List<Task> TaskListForupdateDelete=new List<Task>();
List<Event> eventListForupdateDelete=new List<Event>();
for(Task t:[select id,RecordTypeId,Status,Whatid,Subject from Task where id in:taskList ])
{
t.RecordTypeId= rectype;
t.status='Completed' ;
t.priority='High';
t.subject='sw Etest';
TaskListForupdateDelete.add(t);
}
// system.assertEquals(TaskListForupdateDelete[0].Whatid,AccountList[0].id );
for(Event e:[select id,Whatid,Subject,StartDateTime,EndDateTime from Event where id in:EventList ])
{
eventListForupdateDelete.add(e);
}
system.assertEquals(EventListForupdateDelete[0].Whatid,AccountList[0].id );
update TaskListForupdateDelete;
delete task1;
delete event2;
//
Test.stopTest();
}
}
}
i have wrote the trigger and now i am writting test class for this but code coverage is going only 67%, Please help me to increase the code coverage:
Here is the trigger:
trigger deleteandedittask on Task (before delete, before update) {
string rtypeid = [Select id,Name from RecordType where sObjectType='Task' and isActive=true and Name = 'FA-CRM System Task'].id;
set<Id> bypassProfileIdSet = new Set<id>();
bypassProfileIdSet.add(RoleProfile__c.getInstance('API Users').ID__c);
bypassProfileIdSet.add(RoleProfile__c.getInstance('System Administrator').ID__c);
if(trigger.isUpdate)
{
if(!bypassProfileIdSet.contains(userinfo.getprofileid()))
{
for (task t : Trigger.New)
{
String strwhatId = ''; String strwhoId = '';
If(t.whatid != null)strwhatid = String.valueOf(t.Whatid).substring(0 , 3);
If(t.whoid != null) strwhoid = String.valueOf(t.Whoid).substring(0 , 3);
if(strwhatid == '001' || strwhoid== '003' && t.whatid==null )
{
if( Trigger.oldMap.get(t.id).Status =='Completed')
{
if(t.RecordTypeId == rtypeid)
{
if(Trigger.oldMap.get(t.id).Status != Trigger.newmap.get(t.id).Status)
t.Status.addError('Cannot edit or delete Activity History.');
if(Trigger.oldMap.get(t.id).priority != Trigger.newmap.get(t.id).priority)
t.Priority.addError('Cannot edit or delete Activity History.');
if(Trigger.oldMap.get(t.id).Subject != Trigger.newmap.get(t.id).subject)
t.Subject.addError('Cannot edit or delete Activity History.');
if(Trigger.oldMap.get(t.id).Ownerid != Trigger.newmap.get(t.id).Ownerid)
t.Ownerid.addError('Cannot edit or delete Activity History.');
else
t.addError('Cannot edit or delete Activity History');
}
else
t.addError('Cannot edit or delete Activity History');
}
}
}
}
}
if(Trigger.IsDelete)
{
if(!bypassProfileIdSet.contains(userinfo.getprofileid()))
{
for (task t : Trigger.old)
{
String strwhatId = ''; String strwhoId = '';
If(t.whatid != null)strwhatid = String.valueOf(t.Whatid).substring(0 , 3);
If(t.whoid != null) strwhoid = String.valueOf(t.Whoid).substring(0 , 3);
if(strwhatid == '001' || (strwhoid== '003' && t.Whatid==null))
{
if(( Trigger.oldMap.get(t.id).Status =='Completed') ) {
t.addError('Cannot edit or delete Activity History.');
}
}
}
}
}
}
And here is my test class:
private class Testdeleteandedittask {
static testMethod void myUnitTest() {
//Init and Creating data
List<Profile> ProfileList=new List<Profile>();
String Userrecord='FA-CRM Platform';
Profile pro = [SELECT Id FROM profile WHERE name =:Userrecord];
User u1 = new User(alias = 'TaskF', email='newTaskuser@testorg.com', emailencodingkey='UTF-8', lastname='TaskFormat', languagelocalekey='en_US',localesidkey='en_US', profileid = pro.Id,timezonesidkey='America/Los_Angeles', username='newTaskFormat1@testorg.com',LR_Firm_ID__c=2);
insert u1;
Test.startTest();
System.Runas(u1){
String rectype=[Select id,Name from RecordType where sObjectType='Task' and isActive=true and Name = 'Advisor Tasks'].id;
Map<String,RecordType> RecortypeMap=new Map<String,RecordType>();
for(RecordType r:[select id,name from RecordType Where SobjectType = 'Account' and name='Person'])
{
RecortypeMap.put(r.name,r);
}
List<Account> AccountList=new List<Account>();
Account acc1 = new Account(name='account1',Mailing_Line1__c='Line1',PrimaryPhoneType__c='Business',PrimaryPhone__c='5423534', PrimaryPhoneFormat__c='TEST', SecondaryPhoneFormat__c='TEST');
AccountList.add(acc1);
Account acc2 = new Account(name='account2',Mailing_Line1__c='Line2',PrimaryPhoneType__c='Business',PrimaryPhone__c='5423534', PrimaryPhoneFormat__c='TEST', SecondaryPhoneFormat__c='TEST');
AccountList.add(acc2);
insert AccountList;
Profile[] Pr = [Select id from PRofile limit 2];
RoleProfile__c obj1 = new RoleProfile__c();
obj1.Name = 'API Users';
obj1.ID__c = PR[0].id;
obj1.Type__c = 'Profile';
Insert obj1;
RoleProfile__c obj2 = new RoleProfile__c();
obj2.Name = 'System Administrator';
obj2.ID__c = PR[1].id;
obj2.Type__c = 'Profile';
Insert obj2;
List<Task> tasklist = new List<Task>();
for(integer i=0;i<5;i++){
Task task = new Task();
task.Status='In Progress';
task.Priority='Normal';
task.whatid=AccountList[1].id;
task.subject='sw test'+i;
task.Comments__c='Completedd';
task.Description='compl';
tasklist.add(task);
}
Task task1 = new Task();
task1.Status='In Progress';
task1.Priority='Normal';
task1.whatid=AccountList[0].id;
task1.subject='sw test'+7;
task1.Comments__c='Completedd';
task1.Description='compl';
insert task1;
Task task2 = new Task();
task2.Status='In Progress';
task2.Priority='Normal';
task2.whatid=AccountList[0].id;
task2.subject='sw test'+8;
task2.Comments__c='Completedd';
task2.Description='compl';
insert task2;
task2.Priority='High';
task2.subject='sw test'+9;
update task2;
// tasklist.add(task);
insert tasklist;
System.assert(tasklist.size()>0);
List<Event> Eventlist = new List<Event>();
for(integer i=0;i<5;i++){
Event event = new Event();
event.EndDateTime = System.Now()+10;
event.StartDateTime = System.Now()+10;
event.whatid=AccountList[0].id;
event.subject='ev test'+i;
Eventlist.add(event);
}
insert eventlist;
Event event2 = new Event();
event2.EndDateTime = System.Now()+10;
event2.StartDateTime = System.Now()+10;
event2.whatid=AccountList[0].id;
event2.subject='ev test'+7;
insert event2;
System.assert(tasklist.size()>0);
List<Event> NewEventlist = new List<Event>();
for(Event ev23:eventlist){
ev23.Event_Status__c='Completed';
ev23.Comments__c='Closed in Update ';
NewEventlist.add(ev23);
}
update NewEventlist;
System.assert(NewEventlist.size()>0);
List<Task> TaskListForupdateDelete=new List<Task>();
List<Event> eventListForupdateDelete=new List<Event>();
for(Task t:[select id,RecordTypeId,Status,Whatid,Subject from Task where id in:taskList ])
{
t.RecordTypeId= rectype;
t.status='Completed' ;
t.priority='High';
t.subject='sw Etest';
TaskListForupdateDelete.add(t);
}
// system.assertEquals(TaskListForupdateDelete[0].Whatid,AccountList[0].id );
for(Event e:[select id,Whatid,Subject,StartDateTime,EndDateTime from Event where id in:EventList ])
{
eventListForupdateDelete.add(e);
}
system.assertEquals(EventListForupdateDelete[0].Whatid,AccountList[0].id );
update TaskListForupdateDelete;
delete task1;
delete event2;
//
Test.stopTest();
}
}
}
I would also recommend that you split your current test into several parts instead of trying to test all the facets in one monolithic test method. I see at least three different test methods in your current single test method
Each test should follow the following structure:
- Setup of test data. This includes creation of any data needed by your class. Account, Contacts etc
- Starting the test. This is calling Test.startTest() to reset the governor limits
- Calling your class / method
- Stopping the test.This is calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish
- Asserting that your changes have worked
- If you have inserted/updated/deleted data, you need to query for the updates
- Run System.assert, System.assertEquals, System.assertNotEquals to verify that you got the correct data back
If you have any specific problems with your tests, feel free to create a new post or update this post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.NOTE: Please use the "Add a code sample" button (icon <>) when adding code to increase readability and referenceability.
[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/
[4] http://blog.deadlypenguin.com/blog/testing/strategies/