+ Start a Discussion
Akash Garg 2Akash Garg 2 

test classes to write??

Hi 
I'm new to apex.
can anyone help in writing TEST CLASSES of these 3 apex classes.
 
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;
    }
 }
 
global class tridentUpdateStockWebService
{
    Webservice static SFAResponse updateStock(Stock_element[] StockRec) 
    {
        SFAResponse objResponse = new SFAResponse();
        list <Stock_Report__c> StockLst = new list <Stock_Report__c>{};
        Exceptions__c excep = new Exceptions__c();                              // to capture web service failure exceptions.
        
        
        Set<string> sapCustList = new Set<string>();
        for(Stock_element customerR : StockRec)
        {
            if(customerR.CustNo != null && customerR.CustNo != '')
            sapCustList.add(customerR.CustNo);
        }
        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);}
        
        
        for (Integer i=0; i< StockRec.size(); i++)
        {
            Stock_Report__c StockVr = new Stock_Report__c();
            StockVr.Customer_Name_name1_new__c= StockRec[i].Customer_Name_name1_new;
            
            if(mapCust.get(StockRec[i].CustNo) != null)
            StockVr.Customer__c = mapCust.get(StockRec[i].CustNo);
            
            StockVr.Customer_Number__c= StockRec[i].CustNo;   
            StockVr.Special_Stock_No_SONUM_new__c= StockRec[i].Special_Stock_No_SONUM_new;
            StockVr.Total_Stock_Gesme_new__c= StockRec[i].Total_Stock_Gesme_new;
            StockVr.Material_Code_Matnr_new__c= StockRec[i].Material_Code_Matnr_new;
            StockVr.Material_Description_MAktx_new__c= StockRec[i].Material_Description_MAktx_new;
            StockVr.Batch_Charg_new__c= StockRec[i].Batch_Charg_new;
            StockVr.Product_Type_Lv_type_new__c= StockRec[i].Product_Type_Lv_type_new;
            StockVr.Plant_Werks_new__c= StockRec[i].Plant_Werks_new;
            
            StockLst.add(StockVr);
            system.debug('Values taken'); 
        }
        if (StockLst.size()>0)
     {
        try
        { 
                upsert StockLst;                
                objResponse.Status = true;
                objResponse.Description = 'Record Updated';
                return objResponse;     // this will return result.
        }    
       catch(exception e)
        {
                excep.Record_Id__c = StockLst[0].Customer_Name_name1_new__c;
                excep.error__c = e.getmessage();
                insert excep;
                objResponse.SFDCId = StockLst[0].Customer_Name_name1_new__c;
                objResponse.Status = false;
                objResponse.Description = e.getmessage();
        }
     }
        return objResponse;     //this will return result.               
    }           
    
    global class stock_element
    {
        Webservice string CustNo;
        Webservice string Customer_Name_name1_new;
        Webservice string Special_Stock_No_SONUM_new;
        Webservice string Total_Stock_Gesme_new;
        Webservice string Material_Code_Matnr_new;
        Webservice string Material_Description_MAktx_new;
        Webservice string Batch_Charg_new;
        Webservice string Product_Type_Lv_type_new;
        Webservice string Plant_Werks_new;
        
    }
    
    global class SFAResponse
    {
        webservice String SFDCId;       
        webservice Boolean Status; 
        webservice String Description;
    }

}
 
global class tridentUpdateOverduePGLWebService
{    
    webservice static SFAResponse updateOverdue(Overdue_element[] OverdueRec) 
    {       
        //HttpRequest req = new HttpRequest();   
        //req.setHeader('Content-Type','application/x-www-form-urlencoded');
        SFAResponse objResponse = new SFAResponse();
        Exceptions__c excep = new Exceptions__c();                          // to capture web service failure exceptions.
        list<OverDue_PGL__c> OverdueLst = new list<OverDue_PGL__c>{};
        
    for (Integer i=0; i<OverdueRec.size();i++)
    {
        OverDue_PGL__c OverDueObj= new OverDue_PGL__c();
        
        OverDueObj.CustomerName_name1_new__c= OverdueRec[i].CustomerName_name1_new;
        OverDueObj.Total_Outstanding_INR_DMBTR_new__c = OverdueRec[i].Total_Outstanding_INR_DMBTR_new;
        OverDueObj.Payment_Term_Zterm_new__c = OverdueRec[i].Payment_Term_Zterm_new;
        OverDueObj.Overdue_Amount_INR_ovr_amt_new__c = OverdueRec[i].Overdue_Amount_INR_ovr_amt_new;
        OverDueObj.Ageing_Days_days_new__c = OverdueRec[i].Ageing_Days_days_new;      
        OverDueObj.Excise_Invoice_exnum_new__c = OverdueRec[i].Excise_Invoice_exnum_new;      
        OverDueObj.Item_Text_sgtxt_new__c = OverdueRec[i].Item_Text_sgtxt_new;
                              
        OverdueLst.add(OverDueObj);
        system.debug('Values taken');
  }

    if (Overduelst.size()>0)
    {                     
        try
            {
                insert OverdueLst;                
                objResponse.Status = true;
                objResponse.Description = 'Record Updated';
                system.debug('Record Updated');     
            }    
       catch(exception e)
            {
                system.debug('Error');
                excep.Record_Id__c = OverdueLst[0].CustomerName_name1_new__c;
                excep.error__c = e.getmessage();
                insert excep;
                
              //objResponse.SFDCId = OverdueLst.CustName;
                objResponse.Status = false;
                objResponse.Description = e.getmessage();
            }
     }
     else
     {        
      objResponse.Status = false;
      objResponse.Description ='No Item found to update';
      system.debug('Record not Updated');
     }
   return objResponse;                                                  // this will return result.
   }
        global class Overdue_element
      {
        webservice string CustomerName_name1_new;
        webservice string Total_Outstanding_INR_DMBTR_new;
        webservice string Payment_Term_Zterm_new;
        webservice string Overdue_Amount_INR_ovr_amt_new;
        webservice string Ageing_Days_days_new;
        webservice string Excise_Invoice_exnum_new;
        webservice string Item_Text_sgtxt_new;
    }
     global class SFAResponse
    {
      //webservice Id SFDCId;       
        webservice Boolean Status; 
        webservice String Description;
    }
}

 
Best Answer chosen by Akash Garg 2
Akash Garg 2Akash Garg 2
Hi 

I have written this test class but it only gives 62% code coverage.
can anyone help in increasing code coverage.
@isTest
private class tridentUpdateCFormsWebService_Test
{
   public static testmethod void tridentUpdateCFormsWebService_Test()
    {

         Account a = new Account(); 
         a.Name = 'Test';   
         a.Total_Purchase_of_Relevant_Products_In__c = 123;   
         a.Trident_share_in_total_purchase_In_MTs__c = 123; 
         a.Total_Purchase_of_relevant_products__c = 123;   
         a.BillingPostalCode='1234';
         a.ShippingPostalCode='1456';
         a.BillingCountry = 'India';
         a.billingstate='Delhi';
         a.SAP_Customer_Code__c='5555656';
         a.Payment_Term_on_account_picklist__c='C000-Legacy Upload';
         insert a;
                  
         C_Forms__c cf =new C_Forms__c();
         cf.Account__c=a.id;
         cf.Excise_Invoice_No_vbeln_new__c ='555';
         cf.Customer_Name_name1_new__c ='accsd';
         cf.Quarter_zquarter_new__c ='5';
         cf.Tax_on_sales_and_Purchases_cod_mwskz_new__c ='2';
         cf.Region_regio_new__c ='5';
         cf.VAT_Amount_zvat_amt_new__c ='6';
         cf.Diff_Liability_zdiff_amt_new__c ='4';
         cf.Accounting_Document_Number_belnr_new__c ='9';
         cf.Company_Code_bukrs_new__c  ='56';
         cf.Fiscal_Year_gjahr_new__c ='1';
         cf.Invoice_Value_zinv_value_new__c ='5';
         insert cf;
         
         order__c ord =new order__c();
         ord.Account_Id__c=a.id;
         ord.Address__c='lldsg';
         ord.Agent__c=a.id;
         insert ord; 
         
         Exceptions__c ex =new Exceptions__c();
         ex.Order__c =ord.id;
         ex.Record_Id__c='dst';
         ex.Error__c='efsge';
         insert ex;
               
        List<tridentUpdateCFormsWebService.CForms_element> Listupdate =new List<tridentUpdateCFormsWebService.CForms_element>();
         tridentUpdateCFormsWebService.CForms_element fksg =new tridentUpdateCFormsWebService.CForms_element();
         fksg.Excise_Invoice_No_vbeln_new=cf.Excise_Invoice_No_vbeln_new__c;
         fksg.Customer_Name_name1_new=cf.Customer_Name_name1_new__c;
         fksg.SAP_CustomerId=a.id;
         fksg.Quarter_zquarter_new= cf.Quarter_zquarter_new__c;
         fksg.Tax_on_sales_and_Purchases_cod_mwskz_new=cf.Tax_on_sales_and_Purchases_cod_mwskz_new__c;
         fksg.Region_regio_new= cf.Region_regio_new__c ;
         fksg.VAT_Amount_zvat_amt_new= cf.VAT_Amount_zvat_amt_new__c ;
         fksg.Diff_Liability_zdiff_amt_new= cf.Diff_Liability_zdiff_amt_new__c ;
         fksg.Accounting_Document_Number_belnr_new= cf.Accounting_Document_Number_belnr_new__c ;
         //fksg.SAP_Message='sssegesg';
         fksg.Company_Code_bukrs_new= cf.Company_Code_bukrs_new__c;
         fksg.Fiscal_Year_gjahr_new= cf.Fiscal_Year_gjahr_new__c;
         fksg.Quarter_zquarter_new= cf.Quarter_zquarter_new__c;
         Listupdate.add(fksg);
         
         tridentUpdateCFormsWebService.SFAResponse  sf =new tridentUpdateCFormsWebService.SFAResponse();
         tridentUpdateCFormsWebService.updateCForms(Listupdate);
    }
}

 

All Answers

shankar sudalaimanishankar sudalaimani
@isTest
private class TesttridentUpdateStockWebService{
    static testMethod void TestTesttridentUpdateStock() {
     // insert all required field
   Account TestAccount= new Account();
     TestAccount.name='Test Account';
    TestAccount.Cust_SAP_No__c="123456";
  // insert all required field
  stock_element TestRecord=new stock_element();
  TestRecord.CustNo=TestAccount.Cust_SAP_No__c;
TestRecord.Customer_Name_name1_new="some Name";
// insert all required field
Stock_Report__c StockVr = new Stock_Report__c();
StockVr.Customer_Name_name1_new__c= TestRecord.Customer_Name_name1_new;
StockVr.Customer__c=TestRecord.CustNo;
Test.StartTest();
        upsert StockVr;
 Test.StopTest();
}
}

Please,use this test class and let me know if am correct,I am also a new to salesforce.
Akash Garg 2Akash Garg 2
No, its not working.
because stock_element is not an object.
Akash Garg 2Akash Garg 2
Hi 

I have written this test class but it only gives 62% code coverage.
can anyone help in increasing code coverage.
@isTest
private class tridentUpdateCFormsWebService_Test
{
   public static testmethod void tridentUpdateCFormsWebService_Test()
    {

         Account a = new Account(); 
         a.Name = 'Test';   
         a.Total_Purchase_of_Relevant_Products_In__c = 123;   
         a.Trident_share_in_total_purchase_In_MTs__c = 123; 
         a.Total_Purchase_of_relevant_products__c = 123;   
         a.BillingPostalCode='1234';
         a.ShippingPostalCode='1456';
         a.BillingCountry = 'India';
         a.billingstate='Delhi';
         a.SAP_Customer_Code__c='5555656';
         a.Payment_Term_on_account_picklist__c='C000-Legacy Upload';
         insert a;
                  
         C_Forms__c cf =new C_Forms__c();
         cf.Account__c=a.id;
         cf.Excise_Invoice_No_vbeln_new__c ='555';
         cf.Customer_Name_name1_new__c ='accsd';
         cf.Quarter_zquarter_new__c ='5';
         cf.Tax_on_sales_and_Purchases_cod_mwskz_new__c ='2';
         cf.Region_regio_new__c ='5';
         cf.VAT_Amount_zvat_amt_new__c ='6';
         cf.Diff_Liability_zdiff_amt_new__c ='4';
         cf.Accounting_Document_Number_belnr_new__c ='9';
         cf.Company_Code_bukrs_new__c  ='56';
         cf.Fiscal_Year_gjahr_new__c ='1';
         cf.Invoice_Value_zinv_value_new__c ='5';
         insert cf;
         
         order__c ord =new order__c();
         ord.Account_Id__c=a.id;
         ord.Address__c='lldsg';
         ord.Agent__c=a.id;
         insert ord; 
         
         Exceptions__c ex =new Exceptions__c();
         ex.Order__c =ord.id;
         ex.Record_Id__c='dst';
         ex.Error__c='efsge';
         insert ex;
               
        List<tridentUpdateCFormsWebService.CForms_element> Listupdate =new List<tridentUpdateCFormsWebService.CForms_element>();
         tridentUpdateCFormsWebService.CForms_element fksg =new tridentUpdateCFormsWebService.CForms_element();
         fksg.Excise_Invoice_No_vbeln_new=cf.Excise_Invoice_No_vbeln_new__c;
         fksg.Customer_Name_name1_new=cf.Customer_Name_name1_new__c;
         fksg.SAP_CustomerId=a.id;
         fksg.Quarter_zquarter_new= cf.Quarter_zquarter_new__c;
         fksg.Tax_on_sales_and_Purchases_cod_mwskz_new=cf.Tax_on_sales_and_Purchases_cod_mwskz_new__c;
         fksg.Region_regio_new= cf.Region_regio_new__c ;
         fksg.VAT_Amount_zvat_amt_new= cf.VAT_Amount_zvat_amt_new__c ;
         fksg.Diff_Liability_zdiff_amt_new= cf.Diff_Liability_zdiff_amt_new__c ;
         fksg.Accounting_Document_Number_belnr_new= cf.Accounting_Document_Number_belnr_new__c ;
         //fksg.SAP_Message='sssegesg';
         fksg.Company_Code_bukrs_new= cf.Company_Code_bukrs_new__c;
         fksg.Fiscal_Year_gjahr_new= cf.Fiscal_Year_gjahr_new__c;
         fksg.Quarter_zquarter_new= cf.Quarter_zquarter_new__c;
         Listupdate.add(fksg);
         
         tridentUpdateCFormsWebService.SFAResponse  sf =new tridentUpdateCFormsWebService.SFAResponse();
         tridentUpdateCFormsWebService.updateCForms(Listupdate);
    }
}

 
This was selected as the best answer
Akash Garg 2Akash Garg 2
can anyone help in writing test class.
As i have stuck in this.