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
ethan huntethan hunt 

Need Help in Apex Test Class

Hi All,

Can the below test class be optimized 

if I am having a class
---------------------------------------------------------
public class TVRemoteControl {
    // Volume to be modified
    Integer volume;
    // Constant for maximum volume value
    static final Integer MAX_VOLUME = 50;  
  
    // Constructor
    public TVRemoteControl(Integer v) {
        // Set initial value for volume
        volume = v;
    }
      
    public Integer increaseVolume(Integer amount) {
        volume += amount;
        if (volume > MAX_VOLUME) {
            volume = MAX_VOLUME;
        }
        return volume;
    }
  
    public Integer decreaseVolume(Integer amount) {
        volume -= amount;
        if (volume < 0) {
            volume = 0;
        }
        return volume;
    }  
  
    public static String getMenuOptions() {
        return 'AUDIO SETTINGS - VIDEO SETTINGS';
    }
     
}
--------------------------------------------------------------
When I will write the test case -

@isTest
class TVRemoteControlTest {
    @isTest static void testVolumeIncrease() {
        TVRemoteControl rc = new TVRemoteControl(10);
        Integer newVolume = rc.increaseVolume(15);
        System.assertEquals(25, newVolume);
    }
  
    @isTest static void testVolumeDecrease() {
        TVRemoteControl rc = new TVRemoteControl(20);
        Integer newVolume = rc.decreaseVolume(15);
        System.assertEquals(5, newVolume);      
    }
      
    @isTest static void testVolumeIncreaseOverMax() {
        TVRemoteControl rc = new TVRemoteControl(10);
        Integer newVolume = rc.increaseVolume(100);
        System.assertEquals(50, newVolume);      
    }
  
    @isTest static void testVolumeDecreaseUnderMin() {
        TVRemoteControl rc = new TVRemoteControl(10);
        Integer newVolume = rc.decreaseVolume(100);
        System.assertEquals(0, newVolume);      
    }
  
    @isTest static void testGetMenuOptions() {
        // Static method call. No need to create a class instance.
        String menu = TVRemoteControl.getMenuOptions();
        System.assertNotEquals(null, menu);
        System.assertNotEquals('', menu);
    }
}
--------------------------------------------
Can TVRemoteControl rc = new TVRemoteControl be initialised once and can be reused.

Regards
sfdc_ninjasfdc_ninja
Yes it can.  In a normal Apex class, you would initialize this in a constructor, but as I'm sure you found, you cannot have a consturctor for a test class.  You would want to make use of a static block to achieve this

@isTest
class TVRemoteControlTest {

      private static TVRemoteControl rc;

      static{
            TVRemoteControl rc = new TVRemoteControl(10);
      }

      private static testmethod void testVolumeIncrease() {
            Integer newVolume = rc.increaseVolume(15);
            System.assertEquals(25, newVolume);
      }
 
      private static testmethod void testVolumeDecrease() {
        Integer newVolume = rc.decreaseVolume(15);
        System.assertEquals(5, newVolume);     
      }

     //Rest of your test methods

}
ethan huntethan hunt
Hi Ninija,

Thanks for the help . But if I run the code , I am getting the following error.

Error Error: Compile Error: Duplicate variable: rc (attempt to re-create the variable with type: TVRemoteControl) at line 7 column 28

Regards

sfdc_ninjasfdc_ninja
Yes, sorry.  Silly mistake on my part.

Change this

static{
            TVRemoteControl rc = new TVRemoteControl(10);
}

To this

static{
            rc = new TVRemoteControl(10);
}
Abhi_TripathiAbhi_Tripathi
Hey ,

Refer this post , this will definetly help you out in test class concept

http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html 

Regards,
ABhi Tripathi