You need to sign in to do that
Don't have an account?

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
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
@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
}
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
Change this
static{
TVRemoteControl rc = new TVRemoteControl(10);
}
To this
static{
rc = new TVRemoteControl(10);
}
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