+ Start a Discussion
Akash Garg 2Akash Garg 2 

want test class with code coverage above 75%

Hi 
Can anyone help in writing the test class who gives 100% code coverage .

I Have written a test class, its working but gives only 72% code coverage.

Here is the Apex Class-
global class tridentUpdateCFormsWebService
{    
    webservice static SFAResponse updateCForms(CForms_element[] cfrmRec) {   
        SFAResponse objResponse = new SFAResponse();
        Exceptions__c excep = new Exceptions__c();                              // to capture web service failure exceptions.      
        List<C_Forms__c> cformList = new List<C_Forms__c>();
        List<string> sapCustList = new List<string>();
        for(CForms_element cfrmCust : cfrmRec)
        {
            if(cfrmCust.SAP_CustomerId != null && cfrmCust.SAP_CustomerId != '')
            sapCustList.add(cfrmCust.SAP_CustomerId);
        }
        Map<string,string> mapCust = new Map<string,string>();
        for(account cust : [select id, Cust_SAP_No__c from Account where Cust_SAP_No__c in : sapCustList])
        {mapCust.put(cust.Cust_SAP_No__c,cust.Id);}
        if(cfrmRec[0].SAP_Message == null || cfrmRec[0].SAP_Message == '')
        {
            for(CForms_element cfrm_Rec : cfrmRec)
            {
                C_Forms__c cform_Rec = new C_Forms__c();
                                
                cform_Rec.Excise_Invoice_No_vbeln_new__c = cfrm_Rec.Excise_Invoice_No_vbeln_new;
                cform_Rec.Customer_SAP_Code__c = cfrm_Rec.SAP_CustomerId;
                
                if(mapCust.get(cfrm_Rec.SAP_CustomerId) != null)
                cform_Rec.Account__c = mapCust.get(cfrm_Rec.SAP_CustomerId);
                cform_Rec.Customer_Name_name1_new__c = cfrm_Rec.Customer_Name_name1_new;              
                cform_Rec.Region_regio_new__c = cfrm_Rec.Region_regio_new;                
                cform_Rec.VAT_Amount_zvat_amt_new__c = cfrm_Rec.VAT_Amount_zvat_amt_new;
                cform_Rec.Diff_Liability_zdiff_amt_new__c = cfrm_Rec.Diff_Liability_zdiff_amt_new;
                cform_Rec.Accounting_Document_Number_belnr_new__c = cfrm_Rec.Accounting_Document_Number_belnr_new;              
                cform_Rec.Company_Code_bukrs_new__c = cfrm_Rec.Company_Code_bukrs_new;     
                cform_Rec.Fiscal_Year_gjahr_new__c = cfrm_Rec.Fiscal_Year_gjahr_new;
                cform_Rec.Quarter_zquarter_new__c = cfrm_Rec.Quarter_zquarter_new;
                cform_Rec.Invoice_Value_zinv_value_new__c = cfrm_Rec.Invoice_Value_zinv_value_new;
                cform_Rec.Tax_on_sales_and_Purchases_cod_mwskz_new__c = cfrm_Rec.Tax_on_sales_and_Purchases_cod_mwskz_new;
                                    
                cformList.add(cform_Rec);
            }
            try{
                upsert cformList;
                                                
                objResponse.SFDC_Rec_No = cfrmRec[0].Excise_Invoice_No_vbeln_new;
                objResponse.Status = true;
                return objResponse;
            }
            catch(exception e){
                excep.Record_Id__c = cfrmRec[0].Excise_Invoice_No_vbeln_new;
                //excep.Order__c = cfrmRec[0].cformsId;
                excep.error__c = e.getmessage();
                insert excep;
                
                objResponse.SFDC_Rec_No = cfrmRec[0].Excise_Invoice_No_vbeln_new;
                objResponse.Status = false;
                objResponse.Description = e.getmessage();
                return objResponse;// this will return result.
            }
        }
        else
        {
            list<Exceptions__c> lstexcep = new list<Exceptions__c>();
            for(integer i=0; i < cfrmRec.size(); i++)
            {
                Exceptions__c exp = new Exceptions__c();
                exp.Name = 'E';             
                
                lstexcep.add(exp);
            }
            insert lstexcep;
                       
            objResponse.SFDC_Rec_No = cfrmRec[0].Excise_Invoice_No_vbeln_new;
            objResponse.Status = false;
            return objResponse;// this will return result.
        }                            
    }
    global class CForms_element
    {
        webservice string SAP_CustomerId;        
        webservice string Customer_Name_name1_new;
        webservice string Excise_Invoice_No_vbeln_new;
        webservice string Region_regio_new;
        webservice string VAT_Amount_zvat_amt_new;
        webservice string Diff_Liability_zdiff_amt_new;
        webservice string Accounting_Document_Number_belnr_new;
        webservice string Company_Code_bukrs_new;
        webservice string Fiscal_Year_gjahr_new;
        webservice string Quarter_zquarter_new;
        webservice string Invoice_Value_zinv_value_new;
        webservice string Tax_on_sales_and_Purchases_cod_mwskz_new;
        webservice string SAP_Message;
    } 
    global class SFAResponse
    {
        webservice String SFDC_Rec_No;       
        webservice Boolean Status; 
        webservice String Description;
    }
 }

and here is the test class i have written:-
@isTest
private class tridentUpdateCFormsWebService_Test 
{
    private static testmethod void testCase()
    {
        
        C_Forms__c pgl = new C_Forms__c() ; 
            //pgl.Name='CF-1234' ; 
            pgl.Excise_Invoice_No_vbeln_new__c = '125' ;
            pgl.Customer_Name_name1_new__c = 'test' ; 
            pgl.Region_regio_new__c = '1';
            pgl.VAT_Amount_zvat_amt_new__c  = '9';
            pgl.Diff_Liability_zdiff_amt_new__c  = '1';      
            pgl.Accounting_Document_Number_belnr_new__c  = '12';      
            pgl.Company_Code_bukrs_new__c = '123';
            pgl.Fiscal_Year_gjahr_new__c  = '1';
            pgl.Quarter_zquarter_new__c  = '2';
            pgl.Invoice_Value_zinv_value_new__c  = '12';
            pgl.Tax_on_sales_and_Purchases_cod_mwskz_new__c  = '2';
        insert pgl ; 
        
        tridentUpdateCFormsWebService.CForms_element ele = new tridentUpdateCFormsWebService.CForms_element() ;
            ele.Excise_Invoice_No_vbeln_new = '125' ;
            ele.Customer_Name_name1_new = 'test' ; 
            ele.Region_regio_new = 'test';
            ele.VAT_Amount_zvat_amt_new = '1';
            ele.Diff_Liability_zdiff_amt_new = '1';      
            ele.Accounting_Document_Number_belnr_new = '122';      
            ele.Company_Code_bukrs_new = '123';
            ele.Fiscal_Year_gjahr_new = '1';
            ele.Quarter_zquarter_new = '1';
            ele.Invoice_Value_zinv_value_new = '12';
            ele.Tax_on_sales_and_Purchases_cod_mwskz_new = '2';
            ele.SAP_CustomerId = '7564';
        tridentUpdateCFormsWebService.SFAResponse resp = tridentUpdateCFormsWebService.updateCForms(new List<tridentUpdateCFormsWebService.CForms_element>{ele}) ;
    }
    
                            // This method will generate exception so catch block will be covered
    private static testmethod void testCaseException()
    {  
        C_Forms__c pgl = new C_Forms__c() ; 
            //pgl.Name='CF-1234' ; 
            pgl.Excise_Invoice_No_vbeln_new__c = '125' ;
            pgl.Customer_Name_name1_new__c = 'test' ; 
            pgl.Region_regio_new__c = '1';
            pgl.VAT_Amount_zvat_amt_new__c  = '9';
            pgl.Diff_Liability_zdiff_amt_new__c  = '1';      
            pgl.Accounting_Document_Number_belnr_new__c  = '12';      
            pgl.Company_Code_bukrs_new__c = '123';
            pgl.Fiscal_Year_gjahr_new__c  = '1';
            pgl.Quarter_zquarter_new__c  = '2';
            pgl.Invoice_Value_zinv_value_new__c  = '12';
            pgl.Tax_on_sales_and_Purchases_cod_mwskz_new__c  = '2';
        insert pgl ; 
        
        tridentUpdateCFormsWebService.CForms_element ele = new tridentUpdateCFormsWebService.CForms_element() ;
            ele.Excise_Invoice_No_vbeln_new = 'String For Exception' ;
            ele.Customer_Name_name1_new = 'test' ; 
            ele.Region_regio_new = 'test';
            ele.VAT_Amount_zvat_amt_new = '1';
            ele.Diff_Liability_zdiff_amt_new = '1';      
            ele.Accounting_Document_Number_belnr_new = 'String For Exception';      
            ele.Company_Code_bukrs_new = '123';
            ele.Fiscal_Year_gjahr_new = '1';
            ele.Quarter_zquarter_new = '1';
            ele.Invoice_Value_zinv_value_new = '12';
            ele.Tax_on_sales_and_Purchases_cod_mwskz_new = '2'; 
            ele.SAP_CustomerId = '7564';
        tridentUpdateCFormsWebService.SFAResponse resp = tridentUpdateCFormsWebService.updateCForms(new List<tridentUpdateCFormsWebService.CForms_element>{ele}) ;
    }
    
}

can anyone has the resolution for above problem.