+ Start a Discussion
Shiv ShankarShiv Shankar 

How to cover a text coverage for property

Hello friends,

 

1. I am using some properties on my visualforce page, how can i get test coverage for these properties

2. I am setting the system.test.setCurrentPage(My VF Page) but it's not going through property code.

3. i am mentioning code below for which i want test coverage.

 

 

    public Map<string,Basket__c> productGroupCodeToBasketItem{
        get{
            try{
                if(productGroupCodeToBasketItem == null){
                    productGroupCodeToBasketItem = new Map<string,Basket__c>();
                    for(Basket__c rec : [Select id, product__c,Product__r.Name from Basket__c where createdById =: UserInfo.getUserId()]){
                        productGroupCodeToBasketItem.put(rec.Product__r.Name,rec);//Collectin of all items presetn in basket
                    }
                }  
                return productGroupCodeToBasketItem;
            }catch(Exception e){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,e.getMessage()));
                return null;
            }
        }
        set;
    }
    
    
    //Current Request for the user
    public Request__c request{
        get{
            try{
                if(request == null){
                   
                    List<Request__c> requestList = [SELECT Id,Attention_to__c,Address_1__c,Address_2__c,Shipping_Zip_Postal_Code__c, Shipping_City__c,Shipping_State_Province__c ,Shipping_Country__c, Is_Agree_With_Terms_and_Conditions__c FROM Request__c WHERE CreatedById =: UserInfo.getUserId() AND Status__c = 'In Progress'];
                    if(! requestList.isEmpty()){
                        request = new Request__c();
                        request = requestList.get(0);
                    }
                    if(request != null){
                        request.Shipping_Zip_Postal_Code__c = acc.Shipping_Zip_Postal_Code__c;
                        request.Shipping_City__c = acc.Shipping_City__c;
                        request.Shipping_State_Province__c = acc.Shipping_State_Province__c;
                        request.Shipping_Country__c = acc.Shipping_Country__c ;
                    }
                }
                return request;
            }catch(Exception e){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,e.getMessage()));
                return null;
            }
        }
        set;
    }

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma
For covering any of the property or method, you need to invoke them. And yes, In your case same applies to map too.

For map covering map property :
Map<string,Basket__c> productGroupCodeToBasketItem = objMyClass.productGroupCodeToBasketItem();

All Answers

Rahul SharmaRahul Sharma

You would need to invoke the properties for covering them, process is same as for methods.


Example:

 

// class
public class MyClass {
    public Request__c request{
        get{
            List<Request__c> requestList = [SELECT Id,Attention_to__c,Address_1__c,Address_2__c,Shipping_Zip_Postal_Code__c, Shipping_City__c,Shipping_State_Province__c ,Shipping_Country__c, Is_Agree_With_Terms_and_Conditions__c FROM Request__c WHERE CreatedById =: UserInfo.getUserId() AND Status__c = 'In Progress'];

            return request;
        }
        set;
        }
    public MyClass() {
    }
}
// Cover the property
MyClass objMyClass = new MyClass();
Request__c request = objMyClass.request;

 

Shiv ShankarShiv Shankar

What about the Map property ?

Rahul SharmaRahul Sharma
For covering any of the property or method, you need to invoke them. And yes, In your case same applies to map too.

For map covering map property :
Map<string,Basket__c> productGroupCodeToBasketItem = objMyClass.productGroupCodeToBasketItem();
This was selected as the best answer