-1

I am not able to write test class for below code.

public with sharing class Bilcare_Drugs_Execution_Details {
    public String oppId {get; set;}
    public Bilcare_Drugs_Execution_Details (ApexPages.StandardController controller) 
    {
        oppId = ApexPages.currentPage().getParameters().get('Id');       
        supplierFields = new Supplier__c(); //**

        lstSuppliers = new List<chkSuppliers>();                 
            List<OrderItem> tmp = new  List<OrderItem>();
              tmp = [SELECT Form__c,Manufacturer_Distributor__c,Quantity,Registration_No__c, PricebookEntry.Product2.name,PO_Generate__c,Packs_Executed__c,Marks_and_Container_No__c, No_and_Kind_of_Packages__c, Chosen_Supplier_Name__c, Chosen_Vendor_No_of_Packs__c, Choosen_Grand_Total__c, Packs_Executed_Total_Cost__c, Chosen_Vendor__c,Client_P_O_Number__c,Client_PO_Date__c,Client_PO_Amount__c,PO_Currency__c,Equiv_in_Rs__c,Status_Client_PO__c,Entity_Client_PO__c, Client_PO_Conversion_Rate__c, Chosen_Vendor_Pack_Size__c,Chosen_Vendor_Final_Price_Per_Pack__c
              from OrderItem where order.id=:oppId];

            for(OrderItem s: tmp)
            {
                chkSuppliers c = new chkSuppliers();


                 c.Id = s.Id;
                 c.DrugName = s.PricebookEntry.Product2.name;
                 c.Quantity= s.Quantity;
                 c.Form  = s.Form__c;
                 c.Manufacturer = s.Manufacturer_Distributor__c;
                 c.supplier=s.Chosen_Supplier_Name__c;               
                 c.sup = s.Chosen_Vendor__c; 
                 c.noofpacks = String.valueof(s.Chosen_Vendor_No_of_Packs__c); 
                 c.PricePerPk = s.Chosen_Vendor_Final_Price_Per_Pack__c ; 
                 c.StrPriceperpk = String.valueof(s.Chosen_Vendor_Final_Price_Per_Pack__c) ;
                 c.ClientPONum = s.Client_P_O_Number__c ;
                 c.PODate = s.Client_PO_Date__c;
                 c.POAmount =s.Client_PO_Amount__c;
                 c.POAmtCurrency = s.PO_Currency__c ; 
                 c.EquivInRs = s.Equiv_in_Rs__c; 
                 c.Status = s.Status_Client_PO__c;
                 c.Entity = s.Entity_Client_PO__c;
                 c.ConversionRate = String.valueof(s.Client_PO_Conversion_Rate__c);
                 c.Executed = String.valueof(s.Packs_Executed__c);
                 c.VendorPkSize = Integer.ValueOf(s.Chosen_Vendor_Pack_Size__c);
                 c.VendorNoOfPk = Integer.ValueOf(s.Chosen_Vendor_No_of_Packs__c);
                lstSuppliers.add(c);
            }     

        //displaySuppliers();
    }
    public class chkSuppliers
    {        
        public String Id {get; set;}
        public String DrugName {get; set;}
        public Decimal Quantity {get; set;}
        public String Form {get; set;}
        public String Manufacturer {get; set;}
        public string supplier{get;set;}
        public String noofpacks {get;set;}
        public boolean Check {get;set;}
        public String Executed {get;set;}                  
        public string sup{get;set;}
        public String ClientPONum {get;set;}
        public Date PODate {get;set;}
        public Decimal POAmount {get;set;}
        public String POAmtCurrency {get;set;}
        public Decimal EquivInRs{get;set;}
        public String Status {get;set;}
        public String Entity {get;set;}
        public String ConversionRate {get;set;}
        public Integer VendorPkSize {get;set;}
        public Integer VendorNoOfPk {get;set;} 
        public Decimal PricePerPk {get;set;}
        public String StrPriceperpk {get;set;}
        public List<SelectOption> getCurrencyPicklist()
        {
          List<SelectOption> options = new List<SelectOption>();
           options.add(new SelectOption('','--None--'));     
           Schema.DescribeFieldResult fieldResult = OrderItem.PO_Currency__c.getDescribe();
           List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

           for( Schema.PicklistEntry f : ple)
           {
              options.add(new SelectOption(f.getLabel(), f.getValue()));
           }       
           return options;
        }
        public List<SelectOption> getEntityPicklist()
        {
          List<SelectOption> options = new List<SelectOption>();
           options.add(new SelectOption('','--None--'));     
           Schema.DescribeFieldResult fieldResult = OrderItem.Entity_Client_PO__c.getDescribe();
           List<Schema.PicklistEntry> pli = fieldResult.getPicklistValues();

           for( Schema.PicklistEntry f : pli)
           {
              options.add(new SelectOption(f.getLabel(), f.getValue()));
           }       
           return options;
        }            
    }    
    public string SupLookup {get;set;}   
    public boolean disableinvite  {get; set;}
    public List<chkSuppliers> lstSuppliers {get; set;}
    private Set<String> supplierIDs;     
    public Supplier__c supplierFields {get;set;} //**

   /* public PageReference displaySuppliers()
    {         
    return null;
    }    */
     public PageReference Saveaction()
     {                                                   
              List<OrderItem>  tmp1 =  [SELECT Form__c,Manufacturer_Distributor__c,Quantity,Registration_No__c, PricebookEntry.Product2.name,PO_Generate__c,Packs_Executed__c, Marks_and_Container_No__c, No_and_Kind_of_Packages__c,Choosen_Grand_Total__c
              from OrderItem where order.id=:oppId];           
            for(chkSuppliers s: lstSuppliers)
            {
                OrderItem c = new OrderItem();
                c.Id = s.Id;                                
                if(s.Executed!=null && s.Executed.trim()!='')                               
                    c.Packs_Executed__c =  Integer.valueof(s.Executed.trim());                               
                else                                
                    c.Packs_Executed__c = null;

                if(s.Executed!=null && s.Executed.trim()!='' && s.StrPriceperpk !=null && s.StrPriceperpk.trim() !='')
                    c.Client_PO_Amount__c = s.PricePerPk * Integer.valueof(s.Executed.trim());
                else
                    c.Client_PO_Amount__c = null;
                if(s.ConversionRate !=null && s.ConversionRate.trim()!='')
                    c.Client_PO_Conversion_Rate__c = Integer.valueof(s.ConversionRate.trim());
                else
                    c.Client_PO_Conversion_Rate__c = 1;
                c.PO_Currency__c = s.POAmtCurrency;   
                c.Client_P_O_Number__c = s.ClientPONum;
                c.Client_PO_Date__c    = s.PODate;
                c.Status_Client_PO__c   = s.Status;
                c.Entity_Client_PO__c    = s.Entity;                                                                             
                update c;                                             
            }
         PageReference pr = new PageReference('/apex/Bilcare_Client_PO_Details?Id='+oppId+'&act=save');           
         pr.setRedirect(true);
         return pr;        
     }
}

Test Class

@isTest public class testBilDrugExcutiondetail { 
    static testmethod void testDrugExcutiondetail() { 
        Order o=[select id 
                from Order 
                where Id=:'80128000000cvML']; 
        insert o; 

        o.name='test';

        update o; Order up=[select name 
                            from Order 
                            where id=:o.id]; 
        System.assertEquals('test', up.name); 
        Test.setCurrentPageReference(new PageReference('Page.Bilcare_Drugs_Execution_Details')); 
        System.currentPageReference().getParameters().put('id', o.Id); 
        ApexPages.StandardController sc = new ApexPages.StandardController(o); 
        Bilcare_Drugs_Execution_Details d1= new Bilcare_Drugs_Execution_Details(sc); 
        String nextPage =d1.Saveaction().getUrl();

        System.assertEquals('/apex/Bilcare_Drugs_Execution_Details', nextPage); 
    }
}
  • Please post the code into your question, not a comment. You can edit your code. Also, use Ctrl-K or Cmd-K to indent your code so it appears properly. – sfdcfox Nov 14 '16 at 7:08
0

In your test class, you are querying Order object and then updating it. This is not gonna return anything unless you have SeeAllData=True, which is not a good practice btw.

You need to

  1. first create "Order" records in your test class,
  2. make an update, which would fire the logic in your original class
  3. then you may query the sObject to check / assert the results.

Since, you are new to SF, please go through how to write good unit tests and apex test best practices to learn more about unit tests in apex.

| improve this answer | |
0

Possible this is the error:

Order o=[select id from Order where Id=:'80128000000cvML'];

When starting the test class it have no data, therefore this query doesn't return any record and failed. You should first create Order record. e.g: Order o = new Order (Name='TestOrder', fieldName = value...); insert;

| improve this answer | |
  • i have tried with above solution but it gives me same error. – Afroz Kazi Nov 14 '16 at 9:03
  • can you provide me code because i am new in salesforce. – Afroz Kazi Nov 14 '16 at 9:04
0

@isTest public class testBilDrugExcutiondetail { static testmethod void testDrugExcutiondetail() {

Order o= new Order(Name='test1'); insert o;

Account a=new Account(); a.Name='sid'; insert a; o.AccountId=a.id;

ApexPages.StandardController sc = new ApexPages.StandardController(o); Bilcare_Drugs_Execution_Details d1= new Bilcare_Drugs_Execution_Details(sc); d1.Saveaction();

}

}

| improve this answer | |
  • i have tried this but now i am getting this error System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Account Name:You must enter a value: [] – Afroz Kazi Nov 14 '16 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.