+ Start a Discussion
gustavogustavo 

How to test List <Select Option>

I'm trying to improve the code coverage in a class that actually is 76%. In the class I have the following code that is not tested :

 

            Public List <SelectOption> buildingListOption  {get{//2&3
            List<selectOption> buildingList =new List<selectOption>();
            for(integer indx_j=0;indx_j<BuildList.size();indx_j++)
                buildingList.add(new selectOption(BuildList[indx_j].id,BuildList[indx_j].name)); 
                return buildingList;
            }//3
            private set;
            }//2
Taking out this code the coverage raised 5%. This code appears twice in the class then its weight is 10% in my code.

My question is how can I test this code.

 

Gustavo

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

I'm assuming that this is in a controller of some description as its returning selectoptions.

 

To test this, you simply need to access the property.  Something like:

 

MyController ctrl=new MyController();
// set up buildlist here

List<SelectOption> selOpts=ctrl.buildingListOption;

// assert something about selOpts here - e.g. that its the same size as buildlist

 

All Answers

bob_buzzardbob_buzzard

I'm assuming that this is in a controller of some description as its returning selectoptions.

 

To test this, you simply need to access the property.  Something like:

 

MyController ctrl=new MyController();
// set up buildlist here

List<SelectOption> selOpts=ctrl.buildingListOption;

// assert something about selOpts here - e.g. that its the same size as buildlist

 

This was selected as the best answer
LakshmanLakshman

I want to ask a question - Does assert statement increase code coverage?

To my understanding assert only checks two values and is for our satisfaction only.

Bob can you please enlighten me towards this.

 

Thank you!

bob_buzzardbob_buzzard

You are correct - the assert statement doesn't increase code coverage, as it doesn't execute any of the code that you are testing.

 

Assert statements allow you to verify that the code has performed as per its "contract" - thus if the behaviour of the code was changed unintentionally, the next time the unit test is run the assert will cause a failure.

 

If the behaviour of the code is changed intentionally, then the unit tests (and all affected code) would be changed to reflect the new behaviour as part of the development process.

LakshmanLakshman

Thanks Bob.

gustavogustavo

Thanks Bob

NNRNNR
Hi Bob ,

 please go throw below link and send me how to get my test code coverage more than 75%, i wrote test class it gives 70% only.

https://developer.salesforce.com/forums/ForumsMain?id=906F0000000ApnPIAS
Anjali Sharma 87Anjali Sharma 87
Thanks Bob