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
Nikhil kanth NayaniNikhil kanth Nayani 

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);
        }

 
Best Answer chosen by Nikhil kanth Nayani
sunny522sunny522
Hi Nikhil,
 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

AvaneeshAvaneesh
if this will helpfull select as best answer 

@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 
    }
}
 
Nikhil kanth NayaniNikhil kanth Nayani
Hi Avaneesh,

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
 
AvaneeshAvaneesh
Hi Nikhil ,

yes you have to pass all possible senarios which you are handle in your class.
Nikhil kanth NayaniNikhil kanth Nayani
Kindly let us know how to pass all possible senarios in test class.
sunny522sunny522
Hi Nikhil,
 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();
    }
}

 
This was selected as the best answer
AvaneeshAvaneesh
Let suppose your object name DateChart__c right .
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
}



 
Nikhil kanth NayaniNikhil kanth Nayani
Thank you sunny and Avaneesh