Test coverage for a relative date method
Newbie coder here, so I could use some help with test coverage.
I've got a class for creating test/training data in a sandbox. At the beginning of that class, I've got a couple of utility methods to get the dates I'll need. They're meant to be relative to when the class is run. So the method giveMonthInFY figures out what today is, then gives back the first of the month for when we would be make programs that are in the current fiscal year. (We're an education nonprofit with a July to June fiscal year.) So if I spin up a new training sandbox today, in January, I want to create a Fall program for LAST September (9/1/2018). But if I spin one up in July, I would be creating a program for NEXT September (9/1/2019).
To ensure test coverage, however, I need to actually test handing that method dates before and after July 1. I have no idea how to do that since the method actually calls Today(). Help?
Here's my relevant method to the question:
//Takes the desired month (as an integer), gives the first of that month in the current FY
public static date giveMonthInFY(integer monthInt) {
date dateToReturn;
if (System.today().Month() >= 7) {
dateToReturn = date.newinstance(Date.Today().Year(), monthInt, 1);
}
else {
dateToReturn = date.newinstance(Date.Today().Year(), monthInt, 1)-365;
}
return dateToReturn;
}
And here is the test method:
@isTest
public static void testgiveMonthInFY(){
date testDate3 = Date.newinstance(2018, 01,01);
date sept1 = StarWarsTestDataUtil.giveMonthInFY(9);
system.assertEquals(Date.newinstance(2017, 09, 01), sept1);
date testDate4 = Date.newinstance(2018, 07,01);
date sept1FY19 = StarWarsTestDataUtil.giveMonthInFY(9);
system.assertEquals(Date.newinstance(2018, 09, 01), sept1FY19);
}
The test, when run, only hits the portion of the year we are actually in:
apex unit-test code-coverage
New contributor
add a comment |
Newbie coder here, so I could use some help with test coverage.
I've got a class for creating test/training data in a sandbox. At the beginning of that class, I've got a couple of utility methods to get the dates I'll need. They're meant to be relative to when the class is run. So the method giveMonthInFY figures out what today is, then gives back the first of the month for when we would be make programs that are in the current fiscal year. (We're an education nonprofit with a July to June fiscal year.) So if I spin up a new training sandbox today, in January, I want to create a Fall program for LAST September (9/1/2018). But if I spin one up in July, I would be creating a program for NEXT September (9/1/2019).
To ensure test coverage, however, I need to actually test handing that method dates before and after July 1. I have no idea how to do that since the method actually calls Today(). Help?
Here's my relevant method to the question:
//Takes the desired month (as an integer), gives the first of that month in the current FY
public static date giveMonthInFY(integer monthInt) {
date dateToReturn;
if (System.today().Month() >= 7) {
dateToReturn = date.newinstance(Date.Today().Year(), monthInt, 1);
}
else {
dateToReturn = date.newinstance(Date.Today().Year(), monthInt, 1)-365;
}
return dateToReturn;
}
And here is the test method:
@isTest
public static void testgiveMonthInFY(){
date testDate3 = Date.newinstance(2018, 01,01);
date sept1 = StarWarsTestDataUtil.giveMonthInFY(9);
system.assertEquals(Date.newinstance(2017, 09, 01), sept1);
date testDate4 = Date.newinstance(2018, 07,01);
date sept1FY19 = StarWarsTestDataUtil.giveMonthInFY(9);
system.assertEquals(Date.newinstance(2018, 09, 01), sept1FY19);
}
The test, when run, only hits the portion of the year we are actually in:
apex unit-test code-coverage
New contributor
1
check this out for another alternative salesforce.stackexchange.com/questions/61242/… - I never code Date.today() in my code, alwaysUtil.today
so I can coerce the date for purposes of testmethods
– cropredy
6 hours ago
add a comment |
Newbie coder here, so I could use some help with test coverage.
I've got a class for creating test/training data in a sandbox. At the beginning of that class, I've got a couple of utility methods to get the dates I'll need. They're meant to be relative to when the class is run. So the method giveMonthInFY figures out what today is, then gives back the first of the month for when we would be make programs that are in the current fiscal year. (We're an education nonprofit with a July to June fiscal year.) So if I spin up a new training sandbox today, in January, I want to create a Fall program for LAST September (9/1/2018). But if I spin one up in July, I would be creating a program for NEXT September (9/1/2019).
To ensure test coverage, however, I need to actually test handing that method dates before and after July 1. I have no idea how to do that since the method actually calls Today(). Help?
Here's my relevant method to the question:
//Takes the desired month (as an integer), gives the first of that month in the current FY
public static date giveMonthInFY(integer monthInt) {
date dateToReturn;
if (System.today().Month() >= 7) {
dateToReturn = date.newinstance(Date.Today().Year(), monthInt, 1);
}
else {
dateToReturn = date.newinstance(Date.Today().Year(), monthInt, 1)-365;
}
return dateToReturn;
}
And here is the test method:
@isTest
public static void testgiveMonthInFY(){
date testDate3 = Date.newinstance(2018, 01,01);
date sept1 = StarWarsTestDataUtil.giveMonthInFY(9);
system.assertEquals(Date.newinstance(2017, 09, 01), sept1);
date testDate4 = Date.newinstance(2018, 07,01);
date sept1FY19 = StarWarsTestDataUtil.giveMonthInFY(9);
system.assertEquals(Date.newinstance(2018, 09, 01), sept1FY19);
}
The test, when run, only hits the portion of the year we are actually in:
apex unit-test code-coverage
New contributor
Newbie coder here, so I could use some help with test coverage.
I've got a class for creating test/training data in a sandbox. At the beginning of that class, I've got a couple of utility methods to get the dates I'll need. They're meant to be relative to when the class is run. So the method giveMonthInFY figures out what today is, then gives back the first of the month for when we would be make programs that are in the current fiscal year. (We're an education nonprofit with a July to June fiscal year.) So if I spin up a new training sandbox today, in January, I want to create a Fall program for LAST September (9/1/2018). But if I spin one up in July, I would be creating a program for NEXT September (9/1/2019).
To ensure test coverage, however, I need to actually test handing that method dates before and after July 1. I have no idea how to do that since the method actually calls Today(). Help?
Here's my relevant method to the question:
//Takes the desired month (as an integer), gives the first of that month in the current FY
public static date giveMonthInFY(integer monthInt) {
date dateToReturn;
if (System.today().Month() >= 7) {
dateToReturn = date.newinstance(Date.Today().Year(), monthInt, 1);
}
else {
dateToReturn = date.newinstance(Date.Today().Year(), monthInt, 1)-365;
}
return dateToReturn;
}
And here is the test method:
@isTest
public static void testgiveMonthInFY(){
date testDate3 = Date.newinstance(2018, 01,01);
date sept1 = StarWarsTestDataUtil.giveMonthInFY(9);
system.assertEquals(Date.newinstance(2017, 09, 01), sept1);
date testDate4 = Date.newinstance(2018, 07,01);
date sept1FY19 = StarWarsTestDataUtil.giveMonthInFY(9);
system.assertEquals(Date.newinstance(2018, 09, 01), sept1FY19);
}
The test, when run, only hits the portion of the year we are actually in:
apex unit-test code-coverage
apex unit-test code-coverage
New contributor
New contributor
New contributor
asked 9 hours ago
Michael KolodnerMichael Kolodner
284
284
New contributor
New contributor
1
check this out for another alternative salesforce.stackexchange.com/questions/61242/… - I never code Date.today() in my code, alwaysUtil.today
so I can coerce the date for purposes of testmethods
– cropredy
6 hours ago
add a comment |
1
check this out for another alternative salesforce.stackexchange.com/questions/61242/… - I never code Date.today() in my code, alwaysUtil.today
so I can coerce the date for purposes of testmethods
– cropredy
6 hours ago
1
1
check this out for another alternative salesforce.stackexchange.com/questions/61242/… - I never code Date.today() in my code, always
Util.today
so I can coerce the date for purposes of testmethods– cropredy
6 hours ago
check this out for another alternative salesforce.stackexchange.com/questions/61242/… - I never code Date.today() in my code, always
Util.today
so I can coerce the date for purposes of testmethods– cropredy
6 hours ago
add a comment |
1 Answer
1
active
oldest
votes
You could refactor this into a new method that allows you to provide a reference date, rather than always relying on the today()
context in all cases.
// this single parameter method assumes current date
public static date giveMonthInFY(integer monthInt) {
// call the 2 param method with today as the reference date
return giveMonthInFY(monthInt, Date.Today());
}
// this 2 param method allows for injection of a specific date
public static date giveMonthInFY(integer monthInt, date referenceDate) {
date dateToReturn;
if (referenceDate.Month() >= 7) {
dateToReturn = date.newinstance(referenceDate.Year(), monthInt, 1);
}
else {
dateToReturn = date.newinstance(referenceDate.Year(), monthInt, 1)-365;
}
return dateToReturn;
}
I was afraid there wouldn't be any better/easier way. Makes sense though. So off I go to refactor....
– Michael Kolodner
8 hours ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "459"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Michael Kolodner is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f247598%2ftest-coverage-for-a-relative-date-method%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could refactor this into a new method that allows you to provide a reference date, rather than always relying on the today()
context in all cases.
// this single parameter method assumes current date
public static date giveMonthInFY(integer monthInt) {
// call the 2 param method with today as the reference date
return giveMonthInFY(monthInt, Date.Today());
}
// this 2 param method allows for injection of a specific date
public static date giveMonthInFY(integer monthInt, date referenceDate) {
date dateToReturn;
if (referenceDate.Month() >= 7) {
dateToReturn = date.newinstance(referenceDate.Year(), monthInt, 1);
}
else {
dateToReturn = date.newinstance(referenceDate.Year(), monthInt, 1)-365;
}
return dateToReturn;
}
I was afraid there wouldn't be any better/easier way. Makes sense though. So off I go to refactor....
– Michael Kolodner
8 hours ago
add a comment |
You could refactor this into a new method that allows you to provide a reference date, rather than always relying on the today()
context in all cases.
// this single parameter method assumes current date
public static date giveMonthInFY(integer monthInt) {
// call the 2 param method with today as the reference date
return giveMonthInFY(monthInt, Date.Today());
}
// this 2 param method allows for injection of a specific date
public static date giveMonthInFY(integer monthInt, date referenceDate) {
date dateToReturn;
if (referenceDate.Month() >= 7) {
dateToReturn = date.newinstance(referenceDate.Year(), monthInt, 1);
}
else {
dateToReturn = date.newinstance(referenceDate.Year(), monthInt, 1)-365;
}
return dateToReturn;
}
I was afraid there wouldn't be any better/easier way. Makes sense though. So off I go to refactor....
– Michael Kolodner
8 hours ago
add a comment |
You could refactor this into a new method that allows you to provide a reference date, rather than always relying on the today()
context in all cases.
// this single parameter method assumes current date
public static date giveMonthInFY(integer monthInt) {
// call the 2 param method with today as the reference date
return giveMonthInFY(monthInt, Date.Today());
}
// this 2 param method allows for injection of a specific date
public static date giveMonthInFY(integer monthInt, date referenceDate) {
date dateToReturn;
if (referenceDate.Month() >= 7) {
dateToReturn = date.newinstance(referenceDate.Year(), monthInt, 1);
}
else {
dateToReturn = date.newinstance(referenceDate.Year(), monthInt, 1)-365;
}
return dateToReturn;
}
You could refactor this into a new method that allows you to provide a reference date, rather than always relying on the today()
context in all cases.
// this single parameter method assumes current date
public static date giveMonthInFY(integer monthInt) {
// call the 2 param method with today as the reference date
return giveMonthInFY(monthInt, Date.Today());
}
// this 2 param method allows for injection of a specific date
public static date giveMonthInFY(integer monthInt, date referenceDate) {
date dateToReturn;
if (referenceDate.Month() >= 7) {
dateToReturn = date.newinstance(referenceDate.Year(), monthInt, 1);
}
else {
dateToReturn = date.newinstance(referenceDate.Year(), monthInt, 1)-365;
}
return dateToReturn;
}
answered 8 hours ago
Mark PondMark Pond
18.3k13285
18.3k13285
I was afraid there wouldn't be any better/easier way. Makes sense though. So off I go to refactor....
– Michael Kolodner
8 hours ago
add a comment |
I was afraid there wouldn't be any better/easier way. Makes sense though. So off I go to refactor....
– Michael Kolodner
8 hours ago
I was afraid there wouldn't be any better/easier way. Makes sense though. So off I go to refactor....
– Michael Kolodner
8 hours ago
I was afraid there wouldn't be any better/easier way. Makes sense though. So off I go to refactor....
– Michael Kolodner
8 hours ago
add a comment |
Michael Kolodner is a new contributor. Be nice, and check out our Code of Conduct.
Michael Kolodner is a new contributor. Be nice, and check out our Code of Conduct.
Michael Kolodner is a new contributor. Be nice, and check out our Code of Conduct.
Michael Kolodner is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f247598%2ftest-coverage-for-a-relative-date-method%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
check this out for another alternative salesforce.stackexchange.com/questions/61242/… - I never code Date.today() in my code, always
Util.today
so I can coerce the date for purposes of testmethods– cropredy
6 hours ago