+ Start a Discussion
Christopher PezzaChristopher Pezza 

Test Coverage on a function

public void deleteAttachment(){
        Attachment attc = [SELECT Id, Name from Attachment WHERE Id = :attachId];
        delete(attc);
    }
How would I write test coverage for this function?
Best Answer chosen by Christopher Pezza
Shyam BhundiaShyam Bhundia
try this:

//Create Parent for attachment
Account acc = new Account(name = 'Test Acc');
insert acc;

//create attachment
Attachment att=new Attachment();
att.Body=Blob.valueOf('Some content');
att.Name='test.txt';
att.parentId=acc.id;
insert att;

//check if attachment is inserted as expected
Attachment testAtt = [select id from Attachment where id = att.id];
system.assertNotEquals(null, testAtt);

//call the delete attachment method
nCino_CustomerHealthIndividual chic = new nCino_CustomerHealthIndividual();
//pass attachment id to controller
chic.attachID = att.id;
chic.deleteAttachment();

//Test to see that the attachment doesnt exist.
testAtt = [select id from Attachment where id = att.id];
system.assertEquals(null, testAtt);


One thing I noticed in your other reply is that you have hard coded somes IDs, this will cause an issue when deploying code to other environments and cause the test to fail.

All Answers

Shyam BhundiaShyam Bhundia
To test this, you will need to create a parent record for the attachment, then create the attachment record, then finally call your method.

Something like:

//Create Parent for attachment
Account acc = new Account(name = 'Test Acc');
insert acc;

//create attachment
Attachment att=new Attachment();
att.Body=Blob.valueOf('Some content');
att.Name='test.txt';
att.parentId=acc.id;
insert att;

//check if attachment is inserted as expected
Attachment testAtt = [select id from Attachment where id = att.id];
system.assertNotEquals(null, testAtt);

//call the delete attachment method
deleteAttachment()

//Test to see that the attachment doesnt exist.
testAtt = [select id from Attachment where id = att.id];
system.assertEquals(null, testAtt);


Christopher PezzaChristopher Pezza
CHANNEL_ORDERS__Customer__c customer = new CHANNEL_ORDERS__Customer__c(CHANNEL_ORDERS__Customer_City__c = 'Wilmington', 
												CHANNEL_ORDERS__Customer_Company_Name__c = 'The New Bank',	
												CHANNEL_ORDERS__Customer_Country__c = 'US',	
												CHANNEL_ORDERS__Customer_Org_ID__c = '00DG0000000jN8k', 
												CHANNEL_ORDERS__Customer_State__c = 'NC', 
												CHANNEL_ORDERS__Customer_Street__c = '11 Main Street', 
												CHANNEL_ORDERS__Customer_ZIP_Postal_Code__c = '28411');

		insert(customer);

		Account a = new Account(Name='The New Bank', Type='Customer', Industry='Bank', BillingState='NC', Customer__c = customer.Id, nCino_Version__c='1.64.4', Customer_Success_Manager__c = '005a0000007l9zJ', Primary_Product_Specialist__c = '005a000000AIahS', OwnerId = '005a0000009B00f');

		insert(a);

		sfLma__License__c license = new sfLma__License__c(sfLma__Account__c = a.Id, 
														sfLma__Install_Date__c = system.today(),
														sfLma__License_Type__c = 'Editable',
														sfLma__Package_Version__c = 'a0Ra000000Ib1Vx',
														sfLma__Status__c = 'Active',
														sfLma__Subscriber_Org_ID__c = '00DG0000000jN8k',
														sfLma__Used_Licenses__c = 2);
 
 		insert(license);

		Customer_Health__c c = new Customer_Health__c(Account__c=a.Id);

		insert(c);

		nCino_CustomerHealthIndividual chic = new nCino_CustomerHealthIndividual();

		Attachment attc = new Attachment();
		attc.body = Blob.valueOf('This is an attachment');
		attc.Name = 'fake attachment';
		attc.parentId = c.Id;
		insert(attc);

		Attachment testatt = [SELECT id FROM Attachment WHERE id = :attc.id];
		system.assertNotEquals(null, testatt);

		chic.deleteAttachment();

		testatt = [SELECT id FROM Attachment WHERE id = :attc.id];
		System.assertEquals(null, testatt);
This is what i tried and it didn't like it said no row returned
Shyam BhundiaShyam Bhundia
reason is that you are not passing the attachment id to delete to the nCino_CustomerHealthIndividual controller.  In other words how does your method know which attachment to delete?
Christopher PezzaChristopher Pezza
so i need to pass the id through the deleteattachment function? but how would I go about doing that. Im just confused.
Shyam BhundiaShyam Bhundia
can you show the whole code of nCino_CustomerHealthIndividual please
Christopher PezzaChristopher Pezza
its over 20000+ lines of code would it be better to show what i use for just this function?
//Delete Attachments
    public String attachID {get;set;}

public void deleteAttachment(){
        Attachment attc = [SELECT Id, Name from Attachment WHERE Id = :attachId];
        delete(attc);
    }
<apex:actionFunction name="deleteAttachment" action="{!deleteAttachment}" onComplete="window.location.reload()" reRender="jscript" status="loading">
                <apex:param name="attachID" assignTo="{!attachID}" value=""/>
            </apex:actionFunction>
Thats the code for visual force section on how it passes back. Uses a onclick command to pass the id to the javascript that then goes to the asctionfunction


Shyam BhundiaShyam Bhundia
try this:

//Create Parent for attachment
Account acc = new Account(name = 'Test Acc');
insert acc;

//create attachment
Attachment att=new Attachment();
att.Body=Blob.valueOf('Some content');
att.Name='test.txt';
att.parentId=acc.id;
insert att;

//check if attachment is inserted as expected
Attachment testAtt = [select id from Attachment where id = att.id];
system.assertNotEquals(null, testAtt);

//call the delete attachment method
nCino_CustomerHealthIndividual chic = new nCino_CustomerHealthIndividual();
//pass attachment id to controller
chic.attachID = att.id;
chic.deleteAttachment();

//Test to see that the attachment doesnt exist.
testAtt = [select id from Attachment where id = att.id];
system.assertEquals(null, testAtt);


One thing I noticed in your other reply is that you have hard coded somes IDs, this will cause an issue when deploying code to other environments and cause the test to fail.
This was selected as the best answer
Christopher PezzaChristopher Pezza
It Worked Thank You Makes sense Now!