You need to sign in to do that
Don't have an account?
ashish gupta 61
How to Cover Parent.Type = 'Task' in Attachment test class
I am creating a test class for Attachment trigger, but not able to cover this filter Parent.Type = 'Task' mentioned in where cluase so that my test class covering is not increasing.
//this SOQL query in trigger
List<Attachment> lstAttachments = [SELECT Id, Name, Body, ContentType, ParentId, OwnerId From Attachment
WHERE Id = : lstAttIds AND Parent.Type = 'Task' ];
Please suggest how can i cover Parent.Type = 'Task' in test class.
Test class code:
@isTest
public static void attachment_Test(){
Test.startTest();
Task taskObj = new Task(ownerID=userRec.id,ActivityDate=Date.today(),TaskSubtype='Task',Subject='Test subject',Status='Not Started',Priority='Medium',RecordTypeId=TaskRtID,Type='To Do');
insert taskObj;
Attachment attach=new Attachment();
attach.Name='Unit Test Attachment';
Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
attach.body=bodyBlob;
attach.parentId=taskObj.id;
attach.OwnerId = taskObj.OwnerId;
attach.ContentType='text/plain';
attach.IsPrivate=false;
insert attach;
Test.stopTest();
}
//this SOQL query in trigger
List<Attachment> lstAttachments = [SELECT Id, Name, Body, ContentType, ParentId, OwnerId From Attachment
WHERE Id = : lstAttIds AND Parent.Type = 'Task' ];
Please suggest how can i cover Parent.Type = 'Task' in test class.
Test class code:
@isTest
public static void attachment_Test(){
Test.startTest();
Task taskObj = new Task(ownerID=userRec.id,ActivityDate=Date.today(),TaskSubtype='Task',Subject='Test subject',Status='Not Started',Priority='Medium',RecordTypeId=TaskRtID,Type='To Do');
insert taskObj;
Attachment attach=new Attachment();
attach.Name='Unit Test Attachment';
Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
attach.body=bodyBlob;
attach.parentId=taskObj.id;
attach.OwnerId = taskObj.OwnerId;
attach.ContentType='text/plain';
attach.IsPrivate=false;
insert attach;
Test.stopTest();
}
It has resolved by using condition code in triger using Test.isRunningTest() method.
Trigger Code :
List<Attachment> lstAttachments = [SELECT Id, Name, Body, ContentType, ParentId, OwnerId
From Attachment
WHERE Id = : lstAttIds AND Parent.Type = 'Task' ];
if(Test.isRunningTest()){
//Get attachment
lstAttachments = [SELECT Id, Name, Body, ContentType, ParentId, OwnerId
From Attachment
WHERE Id = : lstAttIds ];
}
Thanks,
Ashish Gupta
All Answers
Add Parent.Type = 'Task' on Task Insertion time.
Thanks,
Foram Rana
I have added Type='Task' and TaskSubtype='Task' too while inserting task, but not going to help. Please suggest any other thing.
Test clas Code: Task taskObj = new Task(Type='Task' ,ownerID=userRec.id,ActivityDate=Date.today(),TaskSubtype='Task',Subject='Test subject',Status='Not Started',Priority='Medium',RecordTypeId=TaskRtID);
Database.insert(taskObj);
Thanks,
Ashish Gupta
It has resolved by using condition code in triger using Test.isRunningTest() method.
Trigger Code :
List<Attachment> lstAttachments = [SELECT Id, Name, Body, ContentType, ParentId, OwnerId
From Attachment
WHERE Id = : lstAttIds AND Parent.Type = 'Task' ];
if(Test.isRunningTest()){
//Get attachment
lstAttachments = [SELECT Id, Name, Body, ContentType, ParentId, OwnerId
From Attachment
WHERE Id = : lstAttIds ];
}
Thanks,
Ashish Gupta