0

how to set a date in the test class, Thanks in advance.

visual force page:

<apex:page controller=IntegrationController" docType="html-5.0">
       <apex:outputPanel >
          <div style="font-size:12px; font-weight:bold;">
              select Date: <apex:input type="date" value="{!dt}"/>
          </div>
            </apex:outputPanel>
            <apex:commandButton value="Get Data" action="{!getData}" />

</apex:page>

controller:

    public class IntegrationController{
   public Date dt {get;set;}
    public void getData(){            
    system.debug('in controller ---> '+dt); 
    String dat = dt.year() + '/' + dt.month() + '/' + dt.day();}

test class:

    @isTest
public class IntegrationController_Test {
static testmethod void test1(){
  Date  testDate =Date.newInstance(2020, 10, 28);        
    system.debug('in test class: '+testDate );
  Test.startTest();         
          IntegrationController Data = new IntegrationController();
           Data.getData();
     Test.stopTest();

}

After running the test class I am getting a null value in controller date. Please help me with this issue.

14:16:47:001 USER_DEBUG [8]|DEBUG|in test class: 2020-10-28 00:00:00
14:16:47:065 USER_DEBUG [22]|DEBUG|in controller ---> null
2

Pretty much the same way you set a variable for any other class. Dot notation.

IntegrationController myController = new IntegrationController();
// If the property is public or has a public setter, you can use dot notation 
//   like normal
myController.dt = Date.today();

Test.startTest();
myController.getData();
Test.stopTest();
1
  • you're awesome. Thank you so much for your help. you made my day great !!!
    – Sriram
    Apr 1 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.