+ Start a Discussion
bharathsdsd kumarbharathsdsd kumar 

Test class help needed for following class

Hi,

I have the following class where i am trying to write test class for it which covers only 35% , i am wondering how to cover the following lines , Kindly help me pls

 
MY APEX CLASS :


public class IarCloneAttachment implements Queueable {
	public Id attachmentId;
	public Id newParentId;
	public Set<String> oldIdList;
	public Map<String,String> oldParentId_newParentId_Map;
	public IarCloneAttachment() {
		
	}

	public void execute(QueueableContext context) {
		cloneAttachments();
	}

	public void cloneAttachments() {
        SkipingHelper.skipTriggerIARAttachmentTrigger  = true;
        SkipingHelper.SkippAllTriggersFromAttachment();
        for(Attachment attch :[SELECT Id,ParentId,BodyLength  FROM Attachment WHERE ParentId in:oldIdList]) {
            System.debug('Attachment xxx Heap Size:'+Limits.getHeapSize() + ' Limit:'+Limits.getLimitHeapSize());
        	System.debug('Attachment xxx Id:' + attch.Id + ' size:' + attch.BodyLength);
        	//Note: The following SOQL is inside a for loop. This is by design. Retrieving all the attachments in one soql will throw the heap size limit error.
			List<Attachment> attachmentList = [SELECT Body,ParentId,CreatedDate,ContentType,OwnerId,CreatedById,LastModifiedDate,IsPrivate,Description,Name,LastModifiedById FROM Attachment WHERE Id=:attch.Id];
			if (attachmentList.size()>0) {
				Attachment sAttachment = attachmentList[0].clone(false);
				sAttachment.ParentId = oldParentId_newParentId_Map.get(attachmentList[0].ParentId);
				sAttachment.ownerId = attachmentList[0].OwnerId;
				insert sAttachment;
				sAttachment.Body = null;
            }
        }
	}
}
MY TEST CLASS :

@isTest
public class IarCloneAttachment_Test {  
    static testmethod void IarCloneAttachment_TestMethod()
    {
        User Uu3 = IARTestUtilities.createStandardUser('WrkTs');
        System.RunAs(Uu3) {
            IARTestUtilities.createCustomSettingsForPRMSFDCVDMSGSAM();            
            Account acc = IARTestUtilities.createTestAccount();  
            system.debug('acc:'+acc); 
            
            Opportunity op = IARTestUtilities.createTestOpportunity(acc.id);                     
            system.debug('op:'+op);
            
            Test.startTest();
            IAR_Main__c IAR = IARTestUtilities.createNewTestIARwithSHForms(acc.Id,op.id,'EMS');        
            system.debug('IAR:'+IAR);
            
            
            Blob b = Blob.valueOf('Test Data');
            Attachment attachment = new Attachment();
            attachment.ParentId = acc.Id;
            attachment.Name = 'TestAttachmentforParent.xls';
            attachment.Body = b;
            insert(attachment);
            List<Attachment> attachmentList = new List<Attachment>();
            attachmentList.add(attachment);
            Set<String> oldIdList = new set<String>();
            oldIdList.add('attachment');

            Test.stopTest();  
            IarCloneAttachment iarc = new IarCloneAttachment();
            iarc.cloneAttachments();
            
        }
    }
}
LINES NOT GETTING COVERED:


public void execute(QueueableContext context) {
		cloneAttachments();
	}






**********************************************************************

List<Attachment> attachmentList = [SELECT Body,ParentId,CreatedDate,ContentType,OwnerId,CreatedById,LastModifiedDate,IsPrivate,Description,Name,LastModifiedById FROM Attachment WHERE Id=:attch.Id];
			if (attachmentList.size()>0) {
				Attachment sAttachment = attachmentList[0].clone(false);
				sAttachment.ParentId = oldParentId_newParentId_Map.get(attachmentList[0].ParentId);
				sAttachment.ownerId = attachmentList[0].OwnerId;
				insert sAttachment;
				sAttachment.Body = null;

Kindly help me how to cover those lines pls

Thanks in Advance

 
Best Answer chosen by bharathsdsd kumar
Balaji BondarBalaji Bondar
Hi bharathsdsd,

You can try below code :
Test.StartTest();
   IarCloneAttachment  IarCloneAttachmentObj  = new IarCloneAttachment ();
   ID batchprocessid = Database.executeBatch(IarCloneAttachmentObj  );
Test.StopTest();

Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.