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

how to cover void method in test class
Hi All,
Could please help me in covering if else conditions in test class for below void method. Thanks in advance.
public void nextDateView() {
if(defaultDateView.equalsIgnoreCase('Month')){
startDate = startDate.addMonths(1);
endDate = endDate.addMonths(1);
}
else if(defaultDateView.equalsIgnoreCase('Week')){
startDate = startDate.addDays(7);
endDate = endDate.addDays(7);
}
else if(defaultDateView.equalsIgnoreCase('Day')){
startDate = startDate.addDays(1);
endDate = endDate.addDays(1);
}
Could please help me in covering if else conditions in test class for below void method. Thanks in advance.
public void nextDateView() {
if(defaultDateView.equalsIgnoreCase('Month')){
startDate = startDate.addMonths(1);
endDate = endDate.addMonths(1);
}
else if(defaultDateView.equalsIgnoreCase('Week')){
startDate = startDate.addDays(7);
endDate = endDate.addDays(7);
}
else if(defaultDateView.equalsIgnoreCase('Day')){
startDate = startDate.addDays(1);
endDate = endDate.addDays(1);
}
Try like this
@istest
public class TestingApexClassTestMethod {
static testmethod void testmethod1(){
TestingApexClasstest ta = new TestingApexClasstest(); // here TestingApexClassTest is your class name
assuming defaultDateView is string
defaultDateView = 'Month';
ta.nextDateView(); // the method which you want to call
defaultDateView = 'Week';
ta.nextDateView();
defaultDateView = 'Day';
ta.nextDateView();
}
}
All Answers
@istest
public class TestingApexClassTestMethod {
static testmethod void testmethod1(){
TestingApexClasstest ta = new TestingApexClasstest(); // here TestingApexClassTest is your class name
ta.nextDateView(); // the method which you want to call
}
}
After calling the void method in test class, we want to cover below if else conditions in test class.
if(defaultDateView.equalsIgnoreCase('Month')){
startDate = startDate.addMonths(1);
endDate = endDate.addMonths(1);
}
else if(defaultDateView.equalsIgnoreCase('Week')){
startDate = startDate.addDays(7);
endDate = endDate.addDays(7);
}
else if(defaultDateView.equalsIgnoreCase('Day')){
startDate = startDate.addDays(1);
endDate = endDate.addDays(1);
Thanks
yes you have to pass all possible senarios which you are handle in your class.
Try like this
@istest
public class TestingApexClassTestMethod {
static testmethod void testmethod1(){
TestingApexClasstest ta = new TestingApexClasstest(); // here TestingApexClassTest is your class name
assuming defaultDateView is string
defaultDateView = 'Month';
ta.nextDateView(); // the method which you want to call
defaultDateView = 'Week';
ta.nextDateView();
defaultDateView = 'Day';
ta.nextDateView();
}
}
now
@isTest
public class DateChart__c {
static testMethod void test1()
{
// write here your first test case.... means pass exactly value which handel the first if condition
//now your obj name is DateChart__c so. pass all required field of your object now
DateChart__c Dc = new DateChart__c();
Dc.name='test';
// now in your first if condition defaultDateView.equalsIgnoreCase('Month') right
Dc.defaultDateView='Month';
Dc.startDate.....
Dc.endDate.....call it with his reference
insert Dc;
}
//............Write your second method and handel your second testCase
Cheer's
}