0

I have a site that is being used internally for our team to submit cases to our admin team. I am trying to write a test class for the apex part of it but my code coverage seems to still be at 0%. Was wondering if someone might be able to help me with this as this is my first test class.

VF:

<apex:page standardController="Case" extensions="case_attachment">
<apex:form id="frm">
    <apex:pageBlock title="New Salesforce Case">
        <apex:pageBlockSection columns="1"  title="Salesforce Case -- Red lines represent required fields." collapsible="false">
        </apex:pageBlockSection>
        <strong>Please click the lookup icon (small button to right of field) to search name selection from Salesforce lookup dialog.</strong>
        <apex:pageBlockSection columns="1">
           <apex:inputField value="{!Case.Requestor_Name__c}" required="true"/>
           <br></br>
           </apex:pageBlockSection>
        <strong>Select Problem and High Priority ONLY if the issue is preventing you from continuing work in Salesforce.</strong>
        <apex:pageBlockSection columns="1">
           <apex:inputField value="{!Case.Type}" required="true"/>
           <apex:inputField value="{!Case.Priority}" required="true"/>
        </apex:pageBlockSection>
        <apex:pageBlockSection columns="1" title="Case Details" collapsible="false">
           <apex:inputField value="{!Case.Department__c}" required="true"/>
           <apex:inputField value="{!Case.Salesforce_Object__c}" required="true"/>
           <apex:inputField value="{!Case.Description}" required="true" style="width:50%; height: 60px" />
        </apex:pageBlockSection>
        <apex:pageBlockButtons >
           <apex:commandButton action="{!save}" value="Submit"/>
           <apex:commandButton action="{!cancel}" value="Cancel"/>
       </apex:pageBlockButtons>
    <apex:pageBlockSection title="Upload the Attachment" collapsible="false" dir="LTR" columns="1">
    <div id="upload" class="upload">                                   
        <apex:inputFile id="fileToUpload" value="{!fileBody}" filename="{!fileName}" styleClass="input-file"/>                            
    </div>
    </apex:pageBlockSection>
   </apex:pageBlock>
</apex:form>
</apex:page>

The controller that I am using for the attachment is being used to save the record, add the attachment and then the page is redirected to a thank you page.

Apex:

public class case_attachment{
public case objcase{get;set;}
public Attachment myAttachment{get;set;}
public string fileName{get;set;}
public Blob fileBody{get;set;}

    public case_attachment(Apexpages.standardcontroller controller)
    {
        objcase = (Case)controller.getRecord();
        myAttachment = new Attachment();
    }
    public pagereference save()
    {
      if(myAttachment.Name == null)
        {
        insert objcase;
        }
      if(myAttachment.Name != null)
      {
        insert objcase;
        System.debug('@@@@@fileBody'+fileBody);    
        myAttachment = new Attachment();
              Integer i=0;
              myAttachment .clear();
              myAttachment.Body = fileBody;
              myAttachment.Name = fileName ;
              myAttachment.ParentId = objcase.id;            
              insert myAttachment;   
        }             
        pagereference pr = Page.Thank_You;                          
        pr.setRedirect(true);
        return pr;
    }
}

I was under the impression that I had to make sure that the test class ran through the required fields of the external site but it doesn't seem to show any test coverage.

Test Class:

@isTest
private class Test_case_attachment {

public static testMethod void validatecase_submission() {        
    Case c = new Case();
    case_attachment pageController = new case_attachment(new ApexPages.StandardController(c));
    pageController.save();

    pageController.myAttachment.Name = 'Test';
    pageController.fileBody=Blob.valueOf('Tester 2');

    }

}

Does anyone know what I am doing wrong?

2

In short, you're getting zero coverage because your test never makes any mention of the class that you want to cover.

In your test, rather than inserting the record, you should declare an instance of the controller class, like so:

case_attachment pageController = new case_attachment(new ApexPages.StandardController(c));
pageController.save();

From there on, you can reference any properties or methods of the controller like so pageController.myAttachment.Name = 'foo';

See this documentation for additional help

  • I was able to get to 36% from that so I appreciate it. The link you sent me keeps giving me a 400 page but in this would you need to somehow include an attachment on this to make it so it calls the If statements in there? – Joshua Anderson Nov 30 '16 at 23:11
  • I've updated my answer with an additional sample for the attachment – RenegadeCoder Dec 1 '16 at 13:42
  • I added that as well as pageController.fileBody=Blob.valueOf('Tester 2'); and I am now at 59% but it seems that the bit of code that is not covered is the attachment. Is there a attachment test bit of code. – Joshua Anderson Dec 1 '16 at 18:49
  • insert objcase; System.debug('@@@@@fileBody'+fileBody); myAttachment = new Attachment(); Integer i=0; myAttachment .clear(); myAttachment.Body = fileBody; myAttachment.Name = fileName ; myAttachment.ParentId = objcase.id; insert myAttachment; – Joshua Anderson Dec 1 '16 at 18:51
  • could you update your question with the current version of your test class? – RenegadeCoder Dec 1 '16 at 19:03
0

I was finally able to play around with it enough for it to work.

Test class that worked:

@isTest
private class Test_case_attachment {

public static testMethod void validatecase_submission() {        
    Case myCase = new Case();
    case_attachment pageController = new case_attachment(new ApexPages.StandardController(myCase));

    pageController.myAttachment.Name = 'foo';
    pageController.fileBody=Blob.valueOf('Body Test');
    pageController.fileName = 'Test';
    pageController.save();

    }

}

Thank you for all that helped me with this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.