+ Start a Discussion
Holly Havelka 17Holly Havelka 17 

Help With DateTime Test Class Failing

Hello all,

Having an issue with my first test class failing.  Need help on setting a specific time in my mock data for the test to not fail.

Here is my class:
public class DateHelper {
  public static Long GetTimeStampFromDate(Date inputDate) {
    Long timeStamp = 0;
    DateTime dt;

    if (inputDate == null)
      return timeStamp;

    dt = Datetime.newInstance(
      inputDate.year(),
      inputDate.month(),
      inputDate.day()
    );

    timeStamp = dt.getTime();

    return timeStamp;
  }
}
Here is my test class:
@isTest
public class DateHelperTests {
  @isTest
  static void GivenAValidDateInput_GetTimeStampFromDate_ShouldReturnValidTimeStamp() {
    //Arrange
    Date inputDate = Date.newInstance(2019, 10, 21);
    Long expectedResult = long.valueOf('1571630400000');

    //Act
    Long actualResult = DateHelper.GetTimeStampFromDate(inputDate);

    System.assert(expectedResult == actualResult);
  }

  @isTest
  static void GivenNullDateInput_GetTimeStampFromDate_ShouldReturnZero() {
    //Arrange
    Long expectedResult = 0;

    //Act
    Long actualResult = DateHelper.GetTimeStampFromDate(null);

    System.assert(expectedResult == actualResult);
  }
}
Any help is much appreciated!

 
Best Answer chosen by Holly Havelka 17
Holly Havelka 17Holly Havelka 17
I had the wrong long value:
@isTest
public class DateHelperTests {
  @isTest
  static void GivenAValidDateInput_GetTimeStampFromDate_ShouldReturnValidTimeStamp() {
    //Arrange
    Date inputDate = Date.newInstance(2019, 10, 21);
    Long expectedResult = long.valueOf('1571641200000');

    //Act
    Long actualResult = DateHelper.GetTimeStampFromDate(inputDate);

    System.assert(expectedResult == actualResult);
  }

  @isTest
  static void GivenNullDateInput_GetTimeStampFromDate_ShouldReturnZero() {
    //Arrange
    Long expectedResult = 0;

    //Act
    Long actualResult = DateHelper.GetTimeStampFromDate(null);

    System.assert(expectedResult == actualResult);
  }
}

 

All Answers

Holly Havelka 17Holly Havelka 17
Hi All, I figured it out.  Thanks!
Holly Havelka 17Holly Havelka 17
I had the wrong long value:
@isTest
public class DateHelperTests {
  @isTest
  static void GivenAValidDateInput_GetTimeStampFromDate_ShouldReturnValidTimeStamp() {
    //Arrange
    Date inputDate = Date.newInstance(2019, 10, 21);
    Long expectedResult = long.valueOf('1571641200000');

    //Act
    Long actualResult = DateHelper.GetTimeStampFromDate(inputDate);

    System.assert(expectedResult == actualResult);
  }

  @isTest
  static void GivenNullDateInput_GetTimeStampFromDate_ShouldReturnZero() {
    //Arrange
    Long expectedResult = 0;

    //Act
    Long actualResult = DateHelper.GetTimeStampFromDate(null);

    System.assert(expectedResult == actualResult);
  }
}

 
This was selected as the best answer