-3

I have the following class that works but I need to get full test coverage on it.

public class exportAZCSV {

    public string header{get;set;}
    public List<wrapper> lstwrapper {get; set;}
    public class wrapper{
        public string external_ref_id{get; set;}
        public string usage_type{get; set;}
        public string category{get; set;}
        public string location{get; set;}
        public string start_time{get; set;}
        public string end_time{get; set;}
        public string quantity{get; set;}
        public string uom{get; set;}
        public string usage_attr_1{get; set;}
        public string usage_attr_2{get; set;}
        public string usage_attr_3{get; set;}
        public string usage_desc{get; set;}         
    }

    public string Filetype{get;set;}
    public boolean isExcel {get;set;}  // in case we ever want to do this in Excel
    public boolean isCsv {get;set;}
    public string Filename{get;set;}

    public exportAZCSV(){
            Filetype = '';
            DateTime dt = Datetime.now();
            String formattedDt = dt.format('yyyy-MM-dd\'T\'hh:mm:ss');
            Date currentDay = Date.today();
            Date selectedDate =  currentDay.addMonths(-1);
            Filename = string.valueOf(selectedDate.month())+ '-Billing' + string.valueOf(dt);
            lstwrapper = new List<wrapper>();
            header = 'external_ref_id,usage_type,category,location,start_time,end_time,quantity,uom,usage_attr_1,usage_attr_2,usage_attr_3,usage_desc';                     
    }

    public void exportTo(){
          string queryStringSockets = 'Select Name, Sockets__c from Availability_Zone__c WHERE Account_Contract_Type__c = \'Metapod\'';
          string queryStringStorage = 'Select Name, Storage__c from Availability_Zone__c WHERE Account_Contract_Type__c = \'Metapod\'';
//          system.debug('lstConsignmentSearchData :'+lstConsignmentSearchData.size());
          List<Availability_Zone__c> AvailabilityZoneSockets = DataBase.Query(queryStringSockets);
          List<Availability_Zone__c> AvailabilityZoneStorage = DataBase.Query(queryStringStorage);
        //system.debug('AZ Data :'+AvailabilityZoneFTP.size());

        if(AvailabilityZoneSockets.size()>0){
              for(Availability_Zone__c az :AvailabilityZoneSockets){
                  wrapper w = new wrapper();
                  Date currentDay = Date.today();
                  Date selectedDate =  currentDay.addMonths(-1);
                  Date firstDate = selectedDate.toStartOfMonth();
                  Date lastDate = firstDate.addDays(date.daysInMonth(selectedDate.year() , selectedDate.month())  - 1);
                  Datetime firstDatedt = datetime.newInstance(firstDate.year(),firstdate.month(),firstdate.day());
                  Datetime lastDatedt = datetime.newInstance(lastDate.year(),lastDate.month(),lastDate.day());
                  w.external_ref_id = az.Name ;
                  w.usage_type = 'COPC_compute';
                  w.category = '';
                  w.location = '';
                  w.start_time = string.valueof(firstDatedt.format('yyyy-MM-dd HH:mm:ss'));
                  w.end_time = string.valueOf(lastDatedt.format('yyyy-MM-dd HH:mm:ss'));
                  w.quantity = string.valueOf(az.Sockets__c);
                  w.uom = '';
                  w.usage_attr_1 = '';
                  w.usage_attr_2 = '';
                  w.usage_attr_3 = '';
                  w.usage_desc = '';
                  lstwrapper.add(w);               
              }             
          }
        if(AvailabilityZoneStorage.size()>0){
              for(Availability_Zone__c az :AvailabilityZoneStorage){
                  wrapper w = new wrapper();
                  Date currentDay = Date.today();
                  Date selectedDate =  currentDay.addMonths(-1);
                  Date firstDate = selectedDate.toStartOfMonth();
                  Date lastDate = firstDate.addDays(date.daysInMonth(selectedDate.year() , selectedDate.month())  - 1);
                  Datetime firstDatedt = datetime.newInstance(firstDate.year(),firstdate.month(),firstdate.day());
                  Datetime lastDatedt = datetime.newInstance(lastDate.year(),lastDate.month(),lastDate.day());
                  w.external_ref_id = az.Name ;
                  w.usage_type = 'COPC_storage';
                  w.category = '';
                  w.location = '';
                  w.start_time = string.valueof(firstDatedt.format('yyyy-MM-dd HH:mm:ss'));
                  w.end_time = string.valueOf(lastDatedt.format('yyyy-MM-dd HH:mm:ss'));
                  w.quantity = string.valueOf(az.Storage__c);
                  w.uom = '';
                  w.usage_attr_1 = '';
                  w.usage_attr_2 = '';
                  w.usage_attr_3 = '';
                  w.usage_desc = '';
                  lstwrapper.add(w);               
              }             
          }
        system.debug('lstwrapper :'+lstwrapper.size());
    }



}

I started writing this test code but its unclear to me how you make sure you get full coverage.

@isTest 
private class exportAZCSVTest {
   static testmethod void test() {
     date testDate = date.newInstance(1972, 2, 17);      
     //insert test account  
     Account testAccount = new Account(name='Test Company Name',type='Customer',Account_Type__c='Enterprise');
     insert testAccount;
     //insert test opportunity
     date billingDate = date.newInstance(2015, 4, 1);
     Opportunity testOpportunity = new Opportunity(AccountId = testAccount.Id, name='Test Opp Name',closedate=billingDate, StageName='Closed Won',Partner_Registration__c='TBD');
     insert testOpportunity;
     //insert AZ  
     Availability_Zone__c testAZ = new Availability_Zone__c(name='Test AZ',Account__c = testAccount.Id);
     insert testAZ;
     //insert Test Sockets  
     Socket__c testSocket = new Socket__c(Availability_Zone__c=testAZ.Id, Activation_Date__c=testDate,Socket_Additions__c=12, Client_Requestor_Name__c='Fred',Zendesk_Ticket_Number__c='333');
     insert testSocket;       
     //insert Test Storage  
     Storage__c testStorage = new Storage__c(Availability_Zone__c=testAZ.Id, Activation_Date__c=testDate,Storage_Change_Quantity__c=65 );
     insert testStorage;       
   }}

closed as off-topic by Himanshu, Ratan Paul, Adam Spriggs, Mohith Shrivastava, Boris Bachovski Jan 16 '16 at 3:13

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – Himanshu, Ratan Paul, Adam Spriggs, Mohith Shrivastava, Boris Bachovski
If this question can be reworded to fit the rules in the help center, please edit the question.

2

You need to call the methods of the class to cover those methods:

private class exportAZCSVTest {
   static testmethod void test() {
     date testDate = date.newInstance(1972, 2, 17);      
     //insert test account  
     Account testAccount = new Account(name='Test Company Name',type='Customer',Account_Type__c='Enterprise');
     insert testAccount;
     //insert test opportunity
     date billingDate = date.newInstance(2015, 4, 1);
     Opportunity testOpportunity = new Opportunity(AccountId = testAccount.Id, name='Test Opp Name',closedate=billingDate, StageName='Closed Won',Partner_Registration__c='TBD');
     insert testOpportunity;
     //insert AZ  
     Availability_Zone__c testAZ = new Availability_Zone__c(name='Test AZ',Account__c = testAccount.Id);
     insert testAZ;
     //insert Test Sockets  
     Socket__c testSocket = new Socket__c(Availability_Zone__c=testAZ.Id, Activation_Date__c=testDate,Socket_Additions__c=12, Client_Requestor_Name__c='Fred',Zendesk_Ticket_Number__c='333');
     insert testSocket;       
     //insert Test Storage  
     Storage__c testStorage = new Storage__c(Availability_Zone__c=testAZ.Id, Activation_Date__c=testDate,Storage_Change_Quantity__c=65 );
     insert testStorage; 
     exportAZCSV tempController = new exportAZCSV();
     tempController. exportTo();
     tempController. exportTo();
     system.assertEquals(tempController.lstwrapper.size()>0,true);
   }}

you can also go through these links if you want to learn more about writing test classes: https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods http://blog.jeffdouglas.com/2010/06/02/testing-salesforce-com-controller-extensions/

0

In fact, you need to run ALL the unit test, my way is to run it from developer console the entire class, there it'will show you the highligthed code covered, also, you can go to the apex test execution in Develop- apex test execution, adding your class, running it, it will show you the % .

Hope this help you.

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