+ Start a Discussion
B_kB_k 

Getting 15% of Code Coverage

Someone can help me out in this problem. I wrote the test class but I am getting 15% of code coverage.


Apex Class
Apex Class

Test ClassTest Class
Best Answer chosen by B_k
ravi soniravi soni
Hi B_K,
find your test class with 80% coverage. In this test class, we covered all the methods and we don't need to cover catch the part.
@isTest
public class CaseControllerExtTest {
 @isTest
    public static void insertCase(){   

  Case cs = new Case(Status='New',Origin = 'IT Request Form');   
 
  ApexPages.StandardController sc = new ApexPages.StandardController(cs);  
 CaseControllerExt testCase = new CaseControllerExt(sc);    
  
 testCase.afile = Blob.valueOf('Test Data');     
 testCase.filename = 'test';   
 testCase.ContentType='text/plain';   
 testCase.createCaseWithAttachment();
 }

}

don't forget to mark it as the best answer.
Thank you
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you share the code. As the onew which you shared were screenshots it would be difficult to try the same code and provide the test class for you. If you share the code as the answer I can try the test class and share the same with you.

Thanks,
 
B_kB_k
Hi Sai,

Apex Class

public class CaseControllerExt {
    private final Case acase;
    public Blob afile {get; set;}
    public String contentType {get; set;}
    public String fileName {get; set;}
    public CaseControllerExt(ApexPages.StandardController stdController){
        this.acase=(Case)stdController.getRecord();
        acase.Status = 'New';
        acase.Origin = 'IT Request Form';
    }
    public PageReference createCaseWithAttachment() {
        try{
            insert(acase);
        }catch(System.DMLException e){
            ApexPages.addMessages(e);
        }
        if(afile!=null){
            Attachment attach=new Attachment();
            attach.Body=afile;
            attach.Name=filename;
            attach.ContentType=contentType;
            attach.ParentID=acase.id;
            try {
                insert(attach);
            } catch(System.DMLException e) {
                ApexPages.addMessages(e);
                return null;
            }
        }
        PageReference p = Page.Confirmpage;
        p.setRedirect(true);
        return p;                    
    }        
}
-------------------------------------------------------------------

Test Class

@isTest public
class CaseControllerExtTest{  

   testMethod static void insertCase(){   

  Case cs = new Case(Status='New',Origin = 'IT Request Form');   
 caseList.add(cs);  
  ApexPages.StandardController sc = new ApexPages.StandardController(cs);  
 CaseControllerExt testCase = new CaseControllerExt(sc);     testCase.createCaseWithAttachment();
 testCase.afile = Blob.valueOf('Test Data');   
 testCase.filename = test;   
 testCase.ContentType='text/plain';   
 testCase.ParentID = cs.Id;   
 }
}
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you also paste the code for Confirmpage page as well. Because there is reference for that page in apex class and test class.

Thanks,
 
B_kB_k
This is the VF page



<apex:page standardController="Case" extensions="CaseControllerExt" >  <apex:form >
 
        <apex:pageBlock >
            

                    <apex:selectList styleClass="bloc" style="color:crimson;width:0px;font-size:0;border:0px;background:#f2f2f2;" size="0" value="{!Case.Subject}">
                     <apex:selectOption itemValue="Attached File Regarding My Case" itemLabel="I Need An Import"/>
              </apex:selectList>    
            
            
                <label>Your Name</label> <apex:inputField value="{!Case.Description}"  html-rows="1" html-cols="20" style="width: 200px !important;" required="true" />
<div style="width: 500px; background: ">  &nbsp;</div>

                
                <apex:inputFile accept=".xlsx,.xls,image/*,.doc, .docx,.ppt, .pptx,.txt,.pdf" filename="{!fileName}" contentType="{!contentType}" value="{!afile}" id="inputFile" onchange="checkFileSize(event, id)"/>
            
<div style="width: 500px; background: ">  &nbsp;</div>
               <apex:commandButton value="Submit" id="submit" action="{!createCaseWithAttachment}" onclick="return test();" />
            
            <script>
     function test(){
         var inputFile = document.querySelectorAll("[id*='inputFile']")[0];
         if(inputFile.value.length === 0)
             return false;

         return true;
     }
</script>
   
            
            <script>
    function checkFileSize(event , id ) {
    if(event.target.files[0].size > 2**19) {
        alert('File must be less than 500kb');
        //**Clear input**
         document.getElementById(id).value ='';  
      }
   }
    </script>
            
            
           
            
        </apex:pageBlock>
        
    </apex:form>
</apex:page>






 
ravi soniravi soni
Hi B_K,
find your test class with 80% coverage. In this test class, we covered all the methods and we don't need to cover catch the part.
@isTest
public class CaseControllerExtTest {
 @isTest
    public static void insertCase(){   

  Case cs = new Case(Status='New',Origin = 'IT Request Form');   
 
  ApexPages.StandardController sc = new ApexPages.StandardController(cs);  
 CaseControllerExt testCase = new CaseControllerExt(sc);    
  
 testCase.afile = Blob.valueOf('Test Data');     
 testCase.filename = 'test';   
 testCase.ContentType='text/plain';   
 testCase.createCaseWithAttachment();
 }

}

don't forget to mark it as the best answer.
Thank you
 
This was selected as the best answer
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the below test class. It gives you 88% code coverage.
@isTest 
public class CaseControllerExtTest{  

   testMethod static void insertCase(){   
List<Case> caselist= new List<Case>();
  Case cs = new Case(Status='New',Origin = 'IT Request Form');   
     caseList.add(cs); 
       String tempString = 'TheBlogReaders.com';
Blob afile = Blob.valueOf(tempString);
         
 
      
     
  ApexPages.StandardController sc = new ApexPages.StandardController(cs);  
 CaseControllerExt testCase = new CaseControllerExt(sc);  
      testCase.afile =afile;     
 testCase.filename = 'test';  
       testCase.createCaseWithAttachment();
 //testCase.afile = Blob.valueOf('Test Data');   
 //testCase.filename = test;   
 //testCase.ContentType='text/plain';   
 //testCase.ParentID = cs.Id;   
 }
}
 

If this solution helps, Please mark it as best answer.

Thanks,