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
Mohammed AzarudeenMohammed Azarudeen 

How do I cover apex class variables in Apex Test class?

I want to cover atleast 75% of class. Now am on 74% just need only one percent but i found it hard. One part of my method is not covering that contains variables and its assignments.
if( Discount != 0 && MDiscount != 0)
                {
           
                    Discount = ((UnitRate )*Discount)/100; 
                    MDiscount = ((UnitRate  - Discount)*MDiscount)/100;
                    objvar.Discount__c = Discount;
                    objvar.MDiscount__c = MDiscount;
                }
                else if(Discount != 0 && MDiscount == 0)
                {
                   
                    Discount = ((UnitRate )*Discount)/100; 
                    objvar.Discount__c = Discount;
                }
                else if(Discount == 0 && MDiscount != 0)
                {
                
                    MDiscount = ((UnitRate )*MDiscount)/100;
                    objvar.MDiscount__c= MDiscount;
    
                }
                else
                {
                    objvar.Discount__c = 0;
                    objvar.MDiscount__c = 0;
                }

above apex class code is one part of condition checking. This part is not covering in my Test class. Help me to cover this.
variables are Discount, MDiscount and UnitRate
oleksandr vashchenkooleksandr vashchenko
Could you give full version of code? We can't give you an example without classname and function name. 
ManojjenaManojjena
Hi Mohammed Azarudeen,

Basically if it class level variable ,then you need to use in test class with instance of the class and dot notation.
Like Class_Name clsnm=new Class_Name ();
clsnm.Discount=10;
If it is provate then add @TestVisible before the varibale .

To know more detail ablout the annotation you can  check below link .

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_testvisible.htm   

Thanks
Mnaoj
David Holland 6David Holland 6
objvar.Discount__c = Discount != 0 ? (UnitRate * Discount)/100 : 0;
objvar.MDiscount__c = MDiscount != 0 ? ((UnitRate - objvar.Discount__c) * MDiscount)/100 : 0;

This should cover all of your code and will always fire.
David Holland 6David Holland 6
Just as a side note, which is just me being pedantic, your variables should be called discount, mDiscount and unitRate :)