+ Start a Discussion
Chad MoutesChad Moutes 

Need Help With Test Class

Okay so I have a Trigger that fires after insert of Task. The Trigger works perfectly, I wrote the following Test Class and I'm only getting 75% code Coverage and I need more than that because my org average is only at 57%. Any help would be greatly appreciated.
 
@isTest

public class TestTaskCreationAccount {

    static testMethod void insertNewTask() {
    
        Task taskToCreate = new Task();
        
        taskToCreate.OwnerId = '005i0000004N77x';
        taskToCreate.Subject = 'Attempted Call';
        taskToCreate.Status = 'Completed';
        
        insert taskToCreate;
        
        taskToCreate = [SELECT Status From Task WHERE Id =:taskToCreate.Id];
        System.debug('Status after trigger fired: ' + taskToCreate.Status);
        
        System.assertEquals('Completed', taskToCreate.Status);
        
        delete taskToCreate;
        
    }
    
    
}

 
Best Answer chosen by Chad Moutes
Balaji Chowdary GarapatiBalaji Chowdary Garapati
Three changes i could suggest by looking at your trigger and test class.,

1) Insert a Recruit__c record first with test data, some thing like:

Recruit__c recruit_Record=new (Name='Something',.....);

insert recruit_Record;

2) Create a task using the id from above statement.,

Task Task_Record=new Task(Status='Completed', Subject='Email Sent',WhatId=recruit_Record.id); // In the query from trigger you were querying only the task records which have subject as email sent or email received.
 insert Task_Record;
3) Delete the task record.

Try it!

And instead of hardcoding the task owner you could insert an user an use his id instead as a best practice.

For more info visit:
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_tools_runas.htm

Thanks,
Balaji

All Answers

Balaji Chowdary GarapatiBalaji Chowdary Garapati

@Chad Moutes:

 If you can post your trigger code,  it will he helpful in guiding the right way!

Thanks,
balaji
Chad MoutesChad Moutes
Here is the trigger: Lines 24, 25, 27, and 30 are red in console view code coverage test. 
 
trigger SumEmailActivitesOnRecruit on Task (after insert, after update, after delete) {

    set<Id> set_Id = new set<Id>();
   
    List<Recruit__c>rec_list = new List<Recruit__c>();
    
    if(Trigger.isInsert || Trigger.isUpdate) {
        for(Task T:Trigger.new){
            set_Id.add(T.WhatId);
        }

     }
    else if(Trigger.isDelete){
        for(Task T:Trigger.old){
            set_Id.add(T.WhatId);
        }

     }
     
    if(Trigger.isAfter && (Trigger.isUpdate || Trigger.isInsert || Trigger.isDelete)){
        rec_list=[SELECT Id, Sum_Email_Activities__c, (SELECT Id FROM Tasks WHERE Status = 'Completed' AND (Subject = 'Email Sent' or Subject = 'Email Received')) FROM Recruit__c WHERE Id IN :set_Id];

     for(Recruit__c rec: rec_list){
        if(rec.Tasks.size()>0)
            rec.Sum_Email_Activities__c = rec.Tasks.size();
        else
            rec.Sum_Email_Activities__c = 0;
     }
        if(!rec_list.isEmpty())
            update rec_list;
        }

}

 
Balaji Chowdary GarapatiBalaji Chowdary Garapati
Three changes i could suggest by looking at your trigger and test class.,

1) Insert a Recruit__c record first with test data, some thing like:

Recruit__c recruit_Record=new (Name='Something',.....);

insert recruit_Record;

2) Create a task using the id from above statement.,

Task Task_Record=new Task(Status='Completed', Subject='Email Sent',WhatId=recruit_Record.id); // In the query from trigger you were querying only the task records which have subject as email sent or email received.
 insert Task_Record;
3) Delete the task record.

Try it!

And instead of hardcoding the task owner you could insert an user an use his id instead as a best practice.

For more info visit:
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_tools_runas.htm

Thanks,
Balaji
This was selected as the best answer
Chad MoutesChad Moutes
I keep getting this error

Error Message System.StringException: Invalid id: R.Id
Stack Trace Class.TestTaskCreationRecruit.insertNewTask: line 19, column 1
Balaji Chowdary GarapatiBalaji Chowdary Garapati
Can you paste the code you are using??
Chad MoutesChad Moutes
I was actually able to solve this by fixing a different trigger that had 0% code coverage in my system so I got my average up, thanks for all the help though.
Chad MoutesChad Moutes
Do you think you could help me with my new question though?

https://developer.salesforce.com/forums/#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Apex_Code_Development&criteria=OPENQUESTIONS&id=906F0000000AyN4IAK
Balaji Chowdary GarapatiBalaji Chowdary Garapati
I did it before you asked :) 

Cheers.,