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
mansimansi 

how to write a test class???

hello, Can any one please tell me how to write a test class for the below class.. please help me out.

 

public class offerGenerationController{
    Amex_offer__c amexoffer;
    String selectedRadioOption;
    public Attachment newfile{get;set;}
    
    public void setAmexoffer(Amex_Offer__c amexoffer){
        this.amexoffer = amexoffer;
        system.debug('In the setter **** :'+amexoffer);
    }
    
    public offerGenerationController(ApexPages.StandardController stdController){
    }
    public Amex_Offer__c getAmexoffer(){
        if(amexoffer == null) amexoffer = new Amex_Offer__c();
        system.debug('In the getter **** :'+amexoffer);
        return amexoffer;
    }
    
    public void setSelectedRadioOption(String selectedRadioOption){
        this.selectedRadioOption = selectedRadioOption;
    }
    
    public String getSelectedRadioOption(){
        return selectedRadioOption;
    }
        
    public void uploadFiles(){
        newfile = new Attachment();
        newfile.ParentId = amexoffer.id;
        if(newFile.Body!=null){
            newfile.Name = String.ValueOf(Date.today()) + ' - Customer upload'; 
            insert newfile;            
        }
    }
    
    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Spend1','Spend'));
        options.add(new SelectOption('Spend2','Spend'));
        options.add(new SelectOption('Spend3','Spend')); 
        options.add(new SelectOption('Spend4','Spend'));
        options.add(new SelectOption('Spend5','Spend'));
        return options;
    }
            
}

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Hi Mansi,

 

Read this : http://forceschool.blogspot.com/search/label/Test%20Apex

 

let me know if any issues in it.

All Answers

frelepfrelep

Hi,

 

First, you can just follow the Salesforce.com best practises

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_best_practices.htm

 

And just google few words to find a lot of examples :-)

 

 

Shashikant SharmaShashikant Sharma

Hi Mansi,

 

Read this : http://forceschool.blogspot.com/search/label/Test%20Apex

 

let me know if any issues in it.

This was selected as the best answer
mansimansi

Hey thank you so much.It helped a lot

Shashikant SharmaShashikant Sharma

Your welcome Mansi

mansimansi

Hey can u help me with increasing the code coverage of the below method ... only first few lines are covered.. please tell me how can i make the coverage 100%.

 

Class Name SaveOnEditAmexCampaignCtrlrExt

 

public PageReference fetchOffersForTheSelecetdMerchants(){
        List<String> offerMer = new List<String>();
        // TODO: only Approved / Active offers will be fetched 
        String offerStatus = 'Approved / Active';
        offerWrapperList.clear();
        for(accountWrapper tAccWra: accountWrapperList)
           {
             if(tAccWra.checkboxField){
                 if(tAccWra.se!=null && tAccWra.se!=''){
                      offerMer.add(tAccWra.se);
                 }
             }
           }
           if(offerMer.size()>0){
              // match the MAP CAP with the se number and active offers will be fetched
              List<Amex_Offer__c> lstOffers = [select id, name, Map_Cap__c, Offer_Id__c, Vld_Strt_Ts__c,
                                              Vld_End_Ts__c, Status__c from Amex_Offer__c where Map_Cap__c in: offerMer and Status__c =: offerStatus];
             
             
             MerchantOfferWrapper tempMerOff=new MerchantOfferWrapper();
             boolean flag=false;
              for(Amex_Offer__c amexOffers : lstOffers){
              
                  for(MerchantOfferWrapper  temp:MerchantOfferList )
                  {
                    if(temp.Merchant!= null && temp.Merchant==amexOffers.Map_Cap__c)
                    {
                          offerWrapper ow = new offerWrapper();
                          ow.checkBoxfield = false;
                          ow.mapCap = amexOffers.Map_Cap__c;
                          ow.offerID= amexOffers.Offer_Id__c;
                          ow.startDate = amexOffers.Vld_Strt_Ts__c;
                          ow.endDate= amexOffers.Vld_End_Ts__c;
                          ow.status = amexOffers.Status__c;
                          ow.offerIDfromOfferObject = amexOffers.id;
                          ow.name= amexOffers.name;
                          offerWrapperList.add(ow);
                          temp.OfferInfo.add(ow);
                          isOfferfetched=true;
                          flag=true;
                    }
                    
                  }
                 if(flag==false) 
                 {
                   MerchantOfferWrapper addMerOff=new MerchantOfferWrapper();
                   addMerOff.Merchant=amexOffers.Map_Cap__c;
                   
                   
                          offerWrapper ow = new offerWrapper();
                          ow.checkBoxfield = false;
                          ow.mapCap = amexOffers.Map_Cap__c;
                          ow.offerID= amexOffers.Offer_Id__c;
                          ow.startDate = amexOffers.Vld_Strt_Ts__c;
                          ow.endDate= amexOffers.Vld_End_Ts__c;
                          ow.status = amexOffers.Status__c;
                          ow.offerIDfromOfferObject = amexOffers.id;
                          ow.name= amexOffers.name;
                          isOfferfetched=true;
                          addMerOff.OfferInfo.add(ow);                          
                         MerchantOfferList.add(addMerOff);
                   
                 } 
                        
               
              }
              
              system.debug('==911==' + MerchantOfferList.size());
           }