Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
ShowAll Questionssorted byDate Posted
ram 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;
}
}
}

Amit Chaudhary 8

```@isTest
public class CrntFYTest
{
static testMethod void testMethod1()
{
CrntFY obj = new CrntFY();
}
}```
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html

Thanks,
Amit Chaudhary
Arunkumar 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 d
Thank you Arunkumar