-1

I have written a test class.My test class passed with 54 %.how to improve my percentage.

Test class:

@isTest
public class Sample_ServiceOptionsChange_CLS_Test{
static TestMethod void ServiceOptionsChangeTest () {


    Sample_CountryInfo__c cs = new Sample_CountryInfo__c(Name = 'Algeria' , Sample_C2GURL__c = 'http://dz.co.rplug.renault.com/range',Sample_CountryCode__c = 'DZ' , Sample_Currency__c = 'DZD');
    insert cs;

    ID devRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Sample-OPP-Algeria-RT').getRecordTypeId();
    Opportunity opp = new Opportunity();
    opp.name = 'uma';
    opp.stagename = 'Draft';
    opp.closedate = System.Today();
    opp.RecordTypeId = devRecordTypeId;
    insert opp;
    System.assertEquals('uma',opp.name);

    Product2 pModel=new Product2();
    pModel.Brand__c='Renault';
    pModel.Name='DZ-Captur';
    pModel.Country_code__c='DZ';
    pModel.Parent_Product__c=null;
    pModel.IsActive = true;
    pModel.Market__c='Test';
    pModel.ProductCode='87J';
    insert pModel;

    Product2 pVersion=new Product2();
    pVersion.Name='DZ-Captur';
    pVersion.Parent_Product__c=pModel.id;
    pVersion.Sample_C2G_version_code__c='VEC858_ALGE';
    pVersion.IsActive = true;
    insert pVersion;

    pModel = [Select id,Name,Brand__c,ProductCode,Parent_Product__c,isActive,Country_code__c,SCP_Label__c,Sample_C2GParentProduct__c  from Product2 where Id=:pModel.Id];

    Opportunity_car_set__c oppCarSet = new Opportunity_car_set__c();
    oppCarSet.name = 'uma';
    oppCarSet.Brand__c=pModel.Brand__c;
    oppCarSet.Model__c=pModel.id;
    oppCarSet.Version__c=pVersion.id;
    oppCarSet.opportunity__c = opp.id;
    oppCarSet.Quantity__c = 2;
    oppCarSet.Price_HT__c =420.0;
    oppCarSet.Sample_TVN_Amount__c=89.7;
    insert oppCarSet;
    System.assertEquals(2,oppCarSet.Quantity__c);
    oppCarSet.Price_HT__c =null;
    oppCarSet.Sample_TVN_Amount__c=null;
    update oppCarSet;

    Service__c  newServ = new Service__c();
    newServ.Opportunity_Car_Set__c=oppCarSet.id;
    newServ.CodeC2G__c = 'test-rem';
    newServ.Code__c='test';
    newServ.Label__c='rem';
    newServ.Price_HT__c=5.0;
    insert newServ;

    Test.Starttest();

       MockCallout getJson = new MockCallout('{\"requestedURI\....."}');       
       MockCallout getJson1 = new MockCallout('{\"requestedURI\...."}')  ;   
       MockCallout getJson3 = new MockCallout('{\"marketingModelPresentationtionItem\....."}');

       MockCallout getJson2= new MockCallout('{"requestedURI":"}')   ;   

       FullMultiMock1 mocks = new FullMultiMock1(new MockCallout[]{ getJson, getJson1,getJson3 ,getJson2 });        
       Test.setMock(HttpCalloutMock.class, mocks);
       Test.StopTest();   
      ApexPages.currentPage().getParameters().put('id',newServ.id);
     ApexPages.StandardController controller=new ApexPages.StandardController(newServ);
    Sample_ServiceOptionsChange_CLS servCls=new Sample_ServiceOptionsChange_CLS(controller);
    servCls.getOptions();
     servCls.customSave();



}




}

apex:

public with sharing class Sample_ServiceOptionsChange_CLS{ 
public string strId;
public String strOppid{get;set;}
public String strModelCode {get;set;}
public String strVersionCode {get;set;}
public List<SelectOption> lstOptions {get; private set;}
List<String> lstC2Goptions= new List<String>();
public String strop{get;set;}
public Service__c objService{get;set;}
public boolean showOptionBlock{get;set;}
public string strOptionVal{get;set;}
public double dQuantity{get;set;}
public string strQuantity{get;set;}
public boolean showOptionBlockEmpty{get;set;}
public String strCode {get;set;}
public String strLabel {get;set;}
public Map<String,Double> mapOptionCodePrice = new Map<String,Double>();
public string strcodeC2G{get;set;}

public Sample_ServiceOptionsChange_CLS(ApexPages.StandardController controller) {

    objService = new Service__c();
    objService = [select Opportunity_car_set__c,CodeC2G__c, Quantity__c from Service__c where id=: controller.getId()]; 
    strId=ApexPages.currentPage().getParameters().get('id');
    strOppid=objService.Opportunity_car_set__c;
    strcodeC2G=objService.CodeC2G__c;
    strQuantity=string.valueof(objService.Quantity__c);
    if(strOppid!=null) {
        Opportunity_car_set__c opp=[SELECT Model__r.ProductCode,Version__r.Sample_C2G_version_code__c,Sample_Country_Code__c FROM Opportunity_car_set__c where id=:strOppid];    
        strModelCode    = opp.Model__r.ProductCode;
        strVersionCode  = opp.Version__r.Sample_C2G_version_code__c;
        system.debug('<<<<<selected value'+strModelCode+''+strVersionCode);
    }
    if(strModelCode!=null && strVersionCode!=null  ){
        showOptionBlock=true;
    }else {showOptionBlockEmpty=true;}


  } 


public List<SelectOption> getOptions(){

   Sample_DZC2GOptionDetails_CLS optionValue=new Sample_DZC2GOptionDetails_CLS();
    if(!String.isBlank(strModelCode) && !String.isBlank(strVersionCode)){
        lstC2Goptions=optionValue.getPriceListbasedOnReference(strModelCode,strVersionCode);
    }
    if(strcodeC2G!=null){
        for (Integer i = (lstC2Goptions.size()-1) ; i>= 0 ; i--){
            String s = lstC2Goptions[i];
            if(s.contains(strcodeC2G)){
                lstC2Goptions.remove(i);
            }
        }
     }
    if(lstC2Goptions.size()>0){
        lstOptions = new List<SelectOption>();
        if(strcodeC2G!=null){
         lstOptions.add(new SelectOption('',strcodeC2G));
         }else {
             lstOptions.add(new SelectOption('','--Select--'));
         }
        for(String strC2Goptions:lstC2Goptions) {
            lstOptions .add(new SelectOption(strC2Goptions,strC2Goptions));
        }
    }
  return lstOptions ;
}


public Pagereference customSave(){

       objService.id=strId;
       if(strOptionVal!=null) {

       system.debug('<<strOptionVal'+strOptionVal);

            strCode=strOptionVal.substringbefore('-').trim();
            strLabel =strOptionVal.substringafter('-').trim();
            objService.Code__c=strCode;
            objService.Label__c=strLabel;
            objService.CodeC2G__c=strOptionVal;
            Double dbPriceHT;
            Sample_DZC2GOptionDetails_CLS optionValues =new Sample_DZC2GOptionDetails_CLS();     
            mapOptionCodePrice = optionValues.getOptionPrice(strModelCode,strVersionCode);
            if (mapOptionCodePrice.containsKey(strCode)) {
                dbPriceHT = mapOptionCodePrice.get(strCode);
            }
            if (dbPriceHT!=null) {
                objService.Price_HT__c=dbPriceHT;
            }
        }
        if(strQuantity!=null) {
            objService.Quantity__c=decimal.valueof(strQuantity);
        }

    try{
        update objService;
        return null;
    }catch(Exception ex){
        System.debug('Error in SOQL Fetching'+ex);
        ApexPages.Message msg = new ApexPages.Message(Apexpages.Severity.ERROR, ex.getMessage() );
        ApexPages.addMessage(msg);
        return null;
    }    
      return null;
}

 }

uncovered lines:

enter image description here

enter image description here

Thanks in advance !!

1
  • 5
    how to improve your percentage? Make sure you hit every possible(extra points if you find impossible) scenario. If then you still have uncovered lines ask yourself: Why is this code even there? When is it used?
    – Novarg
    Apr 12, 2016 at 7:22

2 Answers 2

2

In order to improve your code coverage you need to write test cases that exercise the code paths that are not being executed. Looks like your test cases are missing values that need to be populated / tested.. i.e populate stroptionval, strcodeC2G, 1stC2Goptions with valid data..

0
1

At a first glance I would say that lstC2Goptions is empty. So I would say to add more tests where lstC2Goptions it's correctly populated. I don't know the implementation of optionValue.getPriceListbasedOnReference(strModelCode,strVersionCode); but I think that the problem make return some elements from that method, using a more consistent scenario, or mocking the Sample_DZC2GOptionDetails_CLS class.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .