+ Start a Discussion
Salesforce Test 5Salesforce Test 5 

How to increase code coverage

Hi Friends
The following is my Apex class i have written test class for this, i coverd upto 59% i want to make it increase. can you help me to fulfill this.
 
public class FormCurriculumController { 
  

public Purchase_Order__c curriculum {get; set;}
public Boolean privacy {get; set;}
public Boolean saved {get; set;}
public String styleClass {get; set;}
// Allegato
public Transient Blob resume {get; set;}
public String contentType {get; set;}
public String fileName {get; set;}
      
    public boolean showC1RecordType {get;set;}
    public boolean showC2RecordType {get;set;} 
    
    private Id c1RecordTypeId; 
    private Id c2RecordTypeId;


//ublic FormCurriculumController () {
    //curriculum = new Purchase_Order__c();
    //saved = false;
//}

  public FormCurriculumController(ApexPages.StandardController controller) {

        c1RecordTypeId = Schema.SObjectType.Purchase_Order__c.getRecordTypeInfosByName().get('C1').getRecordTypeId();
        c2RecordTypeId = Schema.SObjectType.Purchase_Order__c.getRecordTypeInfosByName().get('C2').getRecordTypeId();
        
        curriculum= new Purchase_Order__c();
        curriculum= (Purchase_Order__c)controller.getRecord();
         
        String isButtonClicked = Apexpages.currentPage().getParameters().get('setDefaultValues');
        if(isButtonClicked == 'true'){
            setDefaultValues();
        }
        
        showC1RecordType = false;
        showC2RecordType = false;
       
        if(curriculum.RecordTypeId == c1RecordTypeId){
            showC1RecordType = true;
            showC2RecordType = false;
        }
        else if(curriculum.RecordTypeId == c2RecordTypeId){
            showC1RecordType = false;
            showC2RecordType = true;
        }
        
    }
    
     public void setDefaultValues(){
        String quoteId = Apexpages.currentPage().getParameters().get('quoteId');
        Quote__c quote = [Select o.id, o.name, o.Quantity_formula__c, o.Opportunity_Product_Detail__r.Opportunity__c,
                    o.Opportunity__c, o.Record_type_name__c,o.Company_Name__c,Opportunity_Product_Detail__r.Opportunity__r.Account__c from Quote__c o WHERE O.id=:quoteId];
        //system.assert(false,quote);     
        curriculum.Opportunity__c = quote.Opportunity_Product_Detail__r.Opportunity__c;
        curriculum.Quote__c = quote.id;
        curriculum.Company__c = quote.Opportunity_Product_Detail__r.Opportunity__r.Account__c;
        
        if(quote.Record_type_name__c == 'C1'){
            curriculum.RecordTypeId = c1RecordTypeId;
        }
        else {
            curriculum.RecordTypeId = c2RecordTypeId;
        }
    }

    


public PageReference save() {
    try {
        // Validazione effettuata qui perchè il required sull'inputCheckbox non è utilizzabile
        if (!privacy) {
        
            ApexPages.Message reqMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Please upload recent Purchase Order  before saving the record');
            ApexPages.addMessage(reqMsg);
            return null;
        }

        // Inserisce il record
        
       
        
        
        insert(curriculum);
        // Inserisce l'allegato
        if (resume != null) {
            Attachment attach = new Attachment();
            attach.Body = resume;
            attach.Name = fileName;
            attach.ContentType= contentType;
            attach.ParentId = curriculum.id;                
            try {
                insert(attach);
                return new PageReference('/'+curriculum.id);

            } catch (Exception ex) {
                // TODO fare il roolback dell'inserimento del curriculum?

                ApexPages.addMessages(ex);
                return null;
            }                    
        }

        //Papagni 03/09/2013 - update curriculum per fare scattare il trigger di after che invia la mail
        update curriculum;

        saved = true;

    } 
    catch(Exception ex) {
        ApexPages.addMessages(ex);  
    }
    return null;   
}
}
@IsTest
public class TestFormCurriculumController  
{
static testMethod void TestMethodFormCurriculumController  ()
{

        account a=new account(name='test',Region__c='East');
        insert a;
        
        Master_Product__c mp=new Master_Product__c(name='test name',Product_Code__c='c2');
        insert mp;
        
        contact c=new contact(firstname='Meenakshmi',lastname='Goswami',Designation__c='Developer',Accountid=a.id);
        insert c;


        Opportunity2__c op=new Opportunity2__c(Name='test1',Account__c=a.id,Master_Product__c=mp.id,Technical_Bid_date__c=date.Today(),Type_of_Business__c='Regular',Contact_Person__c=c.id);
        insert op;
     
        Opportunity_Product_Detail__c opd=new Opportunity_Product_Detail__c(Opportunity__c=op.id,Company__c=a.id,Quantity__c=10,Make__c='Datalogic-Barcode Scanner', Model__c='S4M 203 Dpi');
        insert opd;
        
        Quote__c qt=new Quote__c(Opportunity_Product_Detail__c=opd.id,Basic_Price_per_1000__c=100,Basic_Price_Each_item__c=200,Purpose_of_Sales__c='SEZ',Declaration_form_be_provided__c='No');
        insert qt;
        
        Quote__c qt1=[select Quantity_formula__c from Quote__c where id=:qt.id];
        system.debug('--------Quote quantity--------'+qt1.Quantity_formula__c );
        
        RecordType rt = [Select id,name from RecordType where SobjectType='Purchase_Order__c' and Name='C1'];
        
        Purchase_Order__c sco=new Purchase_Order__c(RecordTypeId=rt.id,Quote__c=qt.id,Opportunity__c=op.id,Sale_Confirmation_Order_Date__c=date.Today());
        sco.Sale_Confirmation_Order_Number__c='100';
      
       
        insert sco;
    
         test.startTest();
         PageReference pageRef = Page.FormCurriculumController ;
         Test.setCurrentPage(pageRef);
         ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(sco);
         
         ApexPages.currentPage().getParameters().put('Id',sco.id);
         FormCurriculumController  objMyController= new FormCurriculumController  (sc);
         
         
         
         
         
         
         /*ApexPages.currentPage().getParameters().put('Id',emp.id);
            ApexPages.currentPage().getParameters().put('setDefaultValues','true');
            ApexPages.currentPage().getParameters().put('id',sco.id);
           
            ApexPages.StandardController stdLead = new ApexPages.StandardController(sco);
            NewSalesConfirmationExtension objMyController  = new NewSalesConfirmationExtension (stdLead);*/
            objMyController.save();
            
         
        test.stopTest();
        
        
        }
static testMethod void TestMethodFormCurriculumController1()
{

 account a=new account(name='test',Region__c='East');
        insert a;
        
        Master_Product__c mp=new Master_Product__c(name='test name',Product_Code__c='c2');
        insert mp;
        
        contact c=new contact(firstname='Meenakshmi',lastname='Goswami',Designation__c='Developer',Accountid=a.id);
        insert c;


        Opportunity2__c op=new Opportunity2__c(Name='test1',Account__c=a.id,Master_Product__c=mp.id,Technical_Bid_date__c=date.Today(),Type_of_Business__c='Regular',Contact_Person__c=c.id);
        insert op;
     
        Opportunity_Product_Detail__c opd=new Opportunity_Product_Detail__c(Opportunity__c=op.id,Company__c=a.id,Quantity__c=10,Make__c='Datalogic-Barcode Scanner', Model__c='S4M 203 Dpi');
        insert opd;
        
        RecordType quoteRt = [Select id,name from RecordType where SobjectType='Quote__c' and Name='C2'];
        
        Quote__c qt=new Quote__c(RecordTypeId=quoteRt.id,Opportunity_Product_Detail__c=opd.id,Basic_Price_per_1000__c=100,Basic_Price_Each_item__c=200,Purpose_of_Sales__c='SEZ',Declaration_form_be_provided__c='No');
        insert qt;
        
        Quote__c qt1=[select Quantity_formula__c from Quote__c where id=:qt.id];
        system.debug('--------Quote quantity--------'+qt1.Quantity_formula__c );
        
        RecordType rt = [Select id,name from RecordType where SobjectType='Purchase_Order__c' and Name='C2'];
        
        Purchase_Order__c sco=new Purchase_Order__c(RecordTypeId=rt.id,Quote__c=qt.id,Opportunity__c=op.id,Sale_Confirmation_Order_Date__c=date.Today());
        sco.Sale_Confirmation_Order_Number__c='100';
        insert sco;
        
         test.startTest();
            //ApexPages.currentPage().getParameters().put('id',sco.id);
            ApexPages.currentPage().getParameters().put('setDefaultValues','true');
            ApexPages.currentPage().getParameters().put('quoteId',qt.id);
            ApexPages.StandardController stdLead = new ApexPages.StandardController(sco);
            FormCurriculumController  objMyController  = new FormCurriculumController (stdLead);
           
            objMyController.attach.body=bodyBlob ;
           
            
            objMyController.save();
            
          
        
        test.stopTest();
        
        }
}


 
Matthew CokeMatthew Coke
have you read the documentation? code coverage is explained pretty well in there. noone is going to write your code for you

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_intro.htm
Salesforce Test 5Salesforce Test 5
Hi Matthew Coke I have written my test class here.i have just asked how to increase my code coverage .i think you do'nt have idea about the test class
Harpreet On CloudHarpreet On Cloud
@MatthewCoke link should give you an idea about how you can increase code coverage. Based on your test class run results, you can check which all lines are not covered and write scenarios in your test class for them and increase code coverage. Ideally speaking, we should not aim at increasing the code coverage; if we make sure that all scenarios of the code are covered as part of the test class, we will automatically get a higher code coverage.

For this specific example, it seems that resume block is not handled in your test class.