function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sriharisfdc reddysriharisfdc reddy 

test code

decimal bu=0;
        decimal se=0;
        decimal tot = 0;
        string tot1;
        total.clear();
       imbalances1=[SELECT sum(BuyFormula__c) BUY,sum(sellFormula__c) SELL, meter_dum__c meter FROM dealsheet__c where End_Date__c>=:datePicker and Start_Date__c<=:datePicker and  meter_dum__c <> null GROUP BY meter_dum__c ];
       for (AggregateResult ar :  imbalances1)  {
           if(ar.get('buy')==null){bu=0;}
           else{bu=(decimal)ar.get('BUY');}
            if(ar.get('SELL')==null){se=0;}
           else{se= (decimal) ar.get('SELL');}
           tot=bu-se;
            system.debug(bu);
           system.debug(se);
          system.debug(tot);
         //    tot1=(string)tot;
           
          total.add(tot);
           // system.debug('total:'+total.get(0));
       }
       }

I am unable to write Test Code for this code ....any one can help me

thanking u... 
Amit Chaudhary 8Amit Chaudhary 8

Please check below post for test class
1) http://amitsalesforce.blogspot.in/search/label/Test%20Class
2) http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html

Sample of test class you see in above link

Please follow below salesforce Best Practice for Test Classes :-

1. Test class must start with @isTest annotation if class class version is more than 25
2. Test environment support @testVisible , @testSetUp as well
3. Unit test is to test particular piece of code working properly or not .
4. Unit test method takes no argument ,commit no data to database ,send no email ,flagged with testMethod keyword .
5. To deploy to production at-least 75% code coverage is required 
6. System.debug statement are not counted as a part of apex code limit.
7. Test method and test classes are not counted as a part of code limit
9. We should not focus on the  percentage of code coverage ,we should make sure that every use case should covered including positive, negative,bulk and single record .
Single Action -To verify that the the single record produces the correct an expected result .
Bulk action -Any apex record trigger ,class or extension must be invoked for 1-200 records .
Positive behavior : Test every expected behavior occurs through every expected permutation , i,e user filled out every correctly data and not go past the limit .
Negative Testcase :-Not to add future date , Not to specify negative amount.
Restricted User :-Test whether a user with restricted access used in your code .
10. Test class should be annotated with @isTest .
11 . @isTest annotation with test method  is equivalent to testMethod keyword .
12. Test method should static and no void return type .
13. Test class and method default access is private ,no matter to add access specifier .
14. classes with @isTest annotation can't be a interface or enum .
15. Test method code can't be invoked by non test request .
16. Stating with salesforce API 28.0 test method can not reside inside non test classes .
17. @Testvisible annotation to make visible private methods inside test classes.
18. Test method can not be used to test web-service call out . Please use call out mock .
19. You can't  send email from test method.
20.User, profile, organization, AsyncApexjob, Corntrigger, RecordType, ApexClass, ApexComponent ,ApexPage we can access without (seeAllData=true) .
21. SeeAllData=true will not work for API 23 version eailer .
22. Accessing static resource test records in test class e,g List<Account> accList=Test.loadData(Account,SobjectType,'ResourceName').
23. Create TestFactory class with @isTest annotation to exclude from organization code size limit .
24. @testSetup to create test records once in a method  and use in every test method in the test class .
25. We can run unit test by using Salesforce Standard UI,Force.com IDE ,Console ,API.
26. Maximum number of test classes run per 24 hour of period is  not grater of 500 or 10 multiplication of test classes of your organization.
27. As apex runs in system mode so the permission and record sharing are not taken into account . So we need to use system.runAs to enforce record sharing .
28. System.runAs will not enforce user permission or field level permission .
29. Every test to runAs count against the total number of DML issued in the process .


Please let us know if this post will help you
 
Uvais KomathUvais Komath

Share the full code.

The way you shared we dont know class name or method name or anything.

We are looking at some keywords,declaration and mismatching braces

sriharisfdc reddysriharisfdc reddy
public class ImbalancesController {

  //  public String imblist { get; set; }
    public list<dealsheet__c> imbalancesList1 { get; set; }
    public list<AggregateResult> imbalances1{ get; set; }
   public list<AggregateResult> imbalances2{ get; set; }
    public list<Decimal> total{ get; set; }
    public string meter{ get; set; }
     public string meter1;
   
    
    public String flow{ get; set; }
    public string tot{ get; set; }
    
   
   /* public void UpImb1() {
        system.debug(meter);
        system.debug(meter1);
       imbalancesList =[SELECT id, Name, Meter_Name__c, Flow_date__c, Total_Buy__c, Total_Sell__c,Imbalance__c FROM Imbalances__C  where Flow_date__c=:datePicker LIMIT 1000  ];
         imbalancesList1 = null;
    
      // system.debug(imbalancesList);
       //system.debug(datePicker);
      // return new PageReference('/google.com');
    }*/
    public void Imbalancelist()
    {
        if (datePicker<>null){
        total = new List<Decimal>();
        decimal bu=0;
        decimal se=0;
        decimal tot = 0;
        string tot1;
        total.clear();
       imbalances1=[SELECT sum(BuyFormula__c) BUY,sum(sellFormula__c) SELL, meter_dum__c meter FROM dealsheet__c where End_Date__c>=:datePicker and Start_Date__c<=:datePicker and  meter_dum__c <> null GROUP BY meter_dum__c ];
       for (AggregateResult ar :  imbalances1)  {
           if(ar.get('buy')==null){bu=0;}
           else{bu=(decimal)ar.get('BUY');}
            if(ar.get('SELL')==null){se=0;}
           else{se= (decimal) ar.get('SELL');}
           tot=bu-se;
            system.debug(bu);
           system.debug(se);
          system.debug(tot);
         //    tot1=(string)tot;
           
          total.add(tot);
           // system.debug('total:'+total.get(0));
       }
       }
        
    }
     public void datasheet()
    {
        
        system.debug(flow);
        system.debug(meter);
        system.debug(meter1);
        system.debug(datePicker);
        {
        imbalancesList1 =[SELECT id,Vlm_Day__c,NG_Pipe_Name__c, Buy_Sell__c,CounterParty_Name__c, Name, Meter_Name__c, Flow_date__c FROM dealsheet__c where Meter_Name__c =: meter and End_Date__c>=:datePicker and Start_Date__c<=:datePicker limit 1000  ];
        meter1=meter;
            meter=null;
             
        }
       
    }

public Date datePicker { get; set; }
     Public List<Imbalances__c> imbalancesList {get;set;}
     public ImbalancesController (){
     //datePicker = date.today();
     imbalancesList1 = null;
     if(imbalancesList==NULL)
      imbalancesList =[SELECT id, Name, Meter_Name__c , Flow_date__c, Total_Buy__c, Total_Sell__c, Imbalance__c FROM Imbalances__C LIMIT 1000];
     }
         
 
}
 
sriharisfdc reddysriharisfdc reddy
@isTest
public class ImbalanceControllerTest{

    public static testMethod void testMyController() {
        
        CP__c testCP = New CP__c();
        testcp.name='testCp';
        testcp.Confirms_Email__c='a@bc.com';
        insert testCp;
        Meter__c m = new Meter__c();
        m.name='test';
        insert m;
       
        NG_Pipe__c ng= new NG_Pipe__c();
        ng.name='testpipe';
        insert ng;
        CP_Trader__c testTrader = new CP_Trader__c();
        testTrader.name='testTrador';
        testTrader.Cp__C=testcp.id;
        Dealsheet__c testDeal= new Dealsheet__c();
        testdeal.Buy_Sell__c='Buy';
        testDeal.Counter_Party__c=testCP.id;
        testDeal.CP_Trader__c=testTrader.id;
        testDeal.Start_Date__c=date.today().addDays(-2);
        testDeal.End_Date__c=date.today().addDays(2);
        testDeal.Vlm_Day__c=20;
        testDeal.RadioBroker__c='Direct';
        testDeal.Price_Type__c='Fixed';
        testDeal.Trade_Date__c=date.today();
        testDeal.NG_Pipe__c=ng.id;
        testDeal.Delivery_Meter__c=m.id;
        insert testdeal;
        decimal bu=10;
        decimal se=10;
        decimal tot =10;
        
 
               System.assertEquals(tot, bu);
        PageReference pageRef = Page.SendMailDealsheet;
        Test.setCurrentPage(pageRef);
        ImbalancesController controller = new ImbalancesController();
        
        
        
        //controller.UpImb1();
        
        controller.Imbalancelist();
        
        controller.datasheet();
        
        
    }
   
    }



but its giving only 45%