I am trying to add code coverage for the method below and keep getting this error. Basically it's a link that deletes the attachment on a table. The method is called from commandlink that passes the attachment id as a parameter. I am a noob and I don't know how to pass the id correctly in the test.
controller
public String attachToDel {get;set;}
public pagereference delAttach()
{
Attachment toDel=new Attachment(id=AttachToDel);
Delete toDel;
}
test
List<Attachment> newAttachments=myPageCon.newAttachments;
newAttachments[0].Name = 'test attachment';
Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
newAttachments[0].body=bodyBlob;
newAttachments[0].parentId=cs.id;
myPagecon.saveAttach();
myPagecon.getcaso();
myPageCon.getcountattach();
System.assertEquals(1, myPagecon.getAttachments().size());
myPageCon.delAttach(); <----This causes the error.
....
Section in the page where I pass the id to commandlink
<apex:commandLink value="{!$Label.Delete_Attachment}" action="{!delAttach}" >
<apex:param name="AttachIdParam" value="{!attachment.id}" assignTo="!attachToDel}"/>
</apex:commandLink>
Thanks in advance.
Attribution to: PepeFloyd
Possible Suggestion/Solution #1
Its is a bit hard to answer correctly without knowing what's happening in your controller code. How does the commandlink populate the attachToDel property ? If a page parameter is used you could query for it and set it like this
ApexPages.currentPage().getParameters().put('parameterID', 'value');
Attribution to: Samuel De Rycke
Possible Suggestion/Solution #2
you're not setting the attachToDel param (which you should change to type Id btw).
...
System.assertEquals(1, myPagecon.getAttachments().size());
//Set the value in the controller
myPageCon.attachToDel = 'whatever Id you want to test deletion of';
myPageCon.delAttach();
Attribution to: Daniel Blackhall
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/2246