+ Start a Discussion
ram dram d 

How to Write test code for Constructor data ??

Public class CrntFY{
public CrntFY() {

Integer currentYear = Datetime.now().year();
Integer currentMonth = Datetime.now().month();


if(currentMonth > 10) {
startDate = Date.newInstance(currentYear, 11, 1);
endDate = Date.newInstance(currentYear + 1, 11, 1);
currentFY = currentYear + 1;
}
else {
startDate = Date.newInstance(currentYear - 1, 11, 1);
endDate = Date.newInstance(currentYear, 11, 1);
currentFY = currentYear;
}
}
}

Thanks in Advance
 
Amit Chaudhary 8Amit Chaudhary 8
Please try below code:-
 
@isTest 
public class CrntFYTest 
{
    static testMethod void testMethod1() 
	{
		CrntFY obj = new CrntFY();
    }
}
Please check below blog for more information in test classes.
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html

Please let us know if this will help you

Thanks,
Amit Chaudhary
Arunkumar RArunkumar R
Hi Ramesh,

Please find the below modifed class and test class.

Class:
Public class CrntFY{

public date startDate;
public date endDate;
public integer currentFY;
public static Integer currentYear;
public static Integer currentMonth;

public CrntFY() {

currentYear = Test.isRunningTest() ? currentYear : Datetime.now().year();
currentMonth = Test.isRunningTest() ? currentMonth : Datetime.now().month();

if(currentMonth > 10) {
startDate = Date.newInstance(currentYear, 11, 1);
endDate = Date.newInstance(currentYear + 1, 11, 1);
currentFY = currentYear + 1;
}
else {
startDate = Date.newInstance(currentYear - 1, 11, 1);
endDate = Date.newInstance(currentYear, 11, 1);
currentFY = currentYear;
}
}
}

Test Class:
@isTest
private class CrntFYTest
{
    static testMethod void buildData()
    {
        CrntFY.currentYear = 2015;
        CrntFY.currentMonth = 11;
        CrntFY cf = new CrntFY();
        
        Test.startTest();
        CrntFY.currentYear = 2015;
        CrntFY.currentMonth = 9;
        CrntFY cf1 = new CrntFY();
        Test.stopTest();
    }
}
ram dram d
Thank you Arunkumar