How to access Class members decorated using { get; set; }
I'm trying to save on boilerplate code so tried to use { get; set; }
idiom, but the function will not resolve.
This is the class with decorated properties.
public class Counter {
private static Integer hits {get { if(hits == null) hits = 0; hits++; return hits; } private set; }
private static Boolean foo {get;set;}
}
My unit test, that will not build, I get the error: Method does not exist or incorrect signature: void getHits() from the type Counter on every assert line.
@isTest public class CounterTest extends AbstractTestCase {
@isTest static void testCounter() {
Test.startTest();
Counter count = new Counter();
assertNotNull(count);
// instance access, case sensitive and insensitive
System.assert(count.getHits() == 1);
System.assert(count.gethits() == 1);
// static access, case sensitive and insensitive
System.assert(Counter.getHits() == 2);
System.assert(Counter.gethits() == 2);
// even trivial example doesn't work
count.setFoo(true);
System.assert(getFoo());
// I even tried direct access,
System.debug('count : ' + Counter.hits);
System.assert(Counter.hits == 1);
Test.stopTest();
}
}
I've using v44 of apex, how on earth are these used?
Yes, there are lot of questions about { get; set; }
but non address this specific point
apex class getter setter
add a comment |
I'm trying to save on boilerplate code so tried to use { get; set; }
idiom, but the function will not resolve.
This is the class with decorated properties.
public class Counter {
private static Integer hits {get { if(hits == null) hits = 0; hits++; return hits; } private set; }
private static Boolean foo {get;set;}
}
My unit test, that will not build, I get the error: Method does not exist or incorrect signature: void getHits() from the type Counter on every assert line.
@isTest public class CounterTest extends AbstractTestCase {
@isTest static void testCounter() {
Test.startTest();
Counter count = new Counter();
assertNotNull(count);
// instance access, case sensitive and insensitive
System.assert(count.getHits() == 1);
System.assert(count.gethits() == 1);
// static access, case sensitive and insensitive
System.assert(Counter.getHits() == 2);
System.assert(Counter.gethits() == 2);
// even trivial example doesn't work
count.setFoo(true);
System.assert(getFoo());
// I even tried direct access,
System.debug('count : ' + Counter.hits);
System.assert(Counter.hits == 1);
Test.stopTest();
}
}
I've using v44 of apex, how on earth are these used?
Yes, there are lot of questions about { get; set; }
but non address this specific point
apex class getter setter
Related: salesforce.stackexchange.com/q/157006/660 - also as noted below, your class properties must be visible outside of the class itself (public, global, or @TestVisible).
– Mark Pond
34 mins ago
add a comment |
I'm trying to save on boilerplate code so tried to use { get; set; }
idiom, but the function will not resolve.
This is the class with decorated properties.
public class Counter {
private static Integer hits {get { if(hits == null) hits = 0; hits++; return hits; } private set; }
private static Boolean foo {get;set;}
}
My unit test, that will not build, I get the error: Method does not exist or incorrect signature: void getHits() from the type Counter on every assert line.
@isTest public class CounterTest extends AbstractTestCase {
@isTest static void testCounter() {
Test.startTest();
Counter count = new Counter();
assertNotNull(count);
// instance access, case sensitive and insensitive
System.assert(count.getHits() == 1);
System.assert(count.gethits() == 1);
// static access, case sensitive and insensitive
System.assert(Counter.getHits() == 2);
System.assert(Counter.gethits() == 2);
// even trivial example doesn't work
count.setFoo(true);
System.assert(getFoo());
// I even tried direct access,
System.debug('count : ' + Counter.hits);
System.assert(Counter.hits == 1);
Test.stopTest();
}
}
I've using v44 of apex, how on earth are these used?
Yes, there are lot of questions about { get; set; }
but non address this specific point
apex class getter setter
I'm trying to save on boilerplate code so tried to use { get; set; }
idiom, but the function will not resolve.
This is the class with decorated properties.
public class Counter {
private static Integer hits {get { if(hits == null) hits = 0; hits++; return hits; } private set; }
private static Boolean foo {get;set;}
}
My unit test, that will not build, I get the error: Method does not exist or incorrect signature: void getHits() from the type Counter on every assert line.
@isTest public class CounterTest extends AbstractTestCase {
@isTest static void testCounter() {
Test.startTest();
Counter count = new Counter();
assertNotNull(count);
// instance access, case sensitive and insensitive
System.assert(count.getHits() == 1);
System.assert(count.gethits() == 1);
// static access, case sensitive and insensitive
System.assert(Counter.getHits() == 2);
System.assert(Counter.gethits() == 2);
// even trivial example doesn't work
count.setFoo(true);
System.assert(getFoo());
// I even tried direct access,
System.debug('count : ' + Counter.hits);
System.assert(Counter.hits == 1);
Test.stopTest();
}
}
I've using v44 of apex, how on earth are these used?
Yes, there are lot of questions about { get; set; }
but non address this specific point
apex class getter setter
apex class getter setter
asked 6 hours ago
Martin of HessleMartin of Hessle
936
936
Related: salesforce.stackexchange.com/q/157006/660 - also as noted below, your class properties must be visible outside of the class itself (public, global, or @TestVisible).
– Mark Pond
34 mins ago
add a comment |
Related: salesforce.stackexchange.com/q/157006/660 - also as noted below, your class properties must be visible outside of the class itself (public, global, or @TestVisible).
– Mark Pond
34 mins ago
Related: salesforce.stackexchange.com/q/157006/660 - also as noted below, your class properties must be visible outside of the class itself (public, global, or @TestVisible).
– Mark Pond
34 mins ago
Related: salesforce.stackexchange.com/q/157006/660 - also as noted below, your class properties must be visible outside of the class itself (public, global, or @TestVisible).
– Mark Pond
34 mins ago
add a comment |
2 Answers
2
active
oldest
votes
From Apex, you reference it the same as if it didn't have a getter or setter defined. The fact that it is private means you will need to use the @TestVisible
annotation if you want to test against it, but also strongly indicates you shouldn't be testing it, or it shouldn't be private. Usually I will use a public getter and private setter.
public Integer myAttribute { get; private set; }
Once you do that, the reference is simply:
system.assertEquals(1, Counter.hits);
Please note that you should always prefer assertEquals(expected, observed)
over assert(expected == observed)
, and that the expected value comes first. Following these rules will give you much clearer assertion failures, as would including a useful message (the optional third parameter).
add a comment |
To clarify the syntax (if hits wasn't static):
count.hits = 1; // calls the "set"
Integer foo = count.hits; // calls the "get"
You can't call count.getHits()
. The calling syntax looks just like accessing a field.
However, since "hits" is static, it is not tied to an instance of your class, but to your class directly. You would access it as Counter.hits.
Then, you'll need to do something about visibility; either annotate with TestVisible, or make the property public.
New contributor
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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%2f247007%2fhow-to-access-class-members-decorated-using-get-set%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
From Apex, you reference it the same as if it didn't have a getter or setter defined. The fact that it is private means you will need to use the @TestVisible
annotation if you want to test against it, but also strongly indicates you shouldn't be testing it, or it shouldn't be private. Usually I will use a public getter and private setter.
public Integer myAttribute { get; private set; }
Once you do that, the reference is simply:
system.assertEquals(1, Counter.hits);
Please note that you should always prefer assertEquals(expected, observed)
over assert(expected == observed)
, and that the expected value comes first. Following these rules will give you much clearer assertion failures, as would including a useful message (the optional third parameter).
add a comment |
From Apex, you reference it the same as if it didn't have a getter or setter defined. The fact that it is private means you will need to use the @TestVisible
annotation if you want to test against it, but also strongly indicates you shouldn't be testing it, or it shouldn't be private. Usually I will use a public getter and private setter.
public Integer myAttribute { get; private set; }
Once you do that, the reference is simply:
system.assertEquals(1, Counter.hits);
Please note that you should always prefer assertEquals(expected, observed)
over assert(expected == observed)
, and that the expected value comes first. Following these rules will give you much clearer assertion failures, as would including a useful message (the optional third parameter).
add a comment |
From Apex, you reference it the same as if it didn't have a getter or setter defined. The fact that it is private means you will need to use the @TestVisible
annotation if you want to test against it, but also strongly indicates you shouldn't be testing it, or it shouldn't be private. Usually I will use a public getter and private setter.
public Integer myAttribute { get; private set; }
Once you do that, the reference is simply:
system.assertEquals(1, Counter.hits);
Please note that you should always prefer assertEquals(expected, observed)
over assert(expected == observed)
, and that the expected value comes first. Following these rules will give you much clearer assertion failures, as would including a useful message (the optional third parameter).
From Apex, you reference it the same as if it didn't have a getter or setter defined. The fact that it is private means you will need to use the @TestVisible
annotation if you want to test against it, but also strongly indicates you shouldn't be testing it, or it shouldn't be private. Usually I will use a public getter and private setter.
public Integer myAttribute { get; private set; }
Once you do that, the reference is simply:
system.assertEquals(1, Counter.hits);
Please note that you should always prefer assertEquals(expected, observed)
over assert(expected == observed)
, and that the expected value comes first. Following these rules will give you much clearer assertion failures, as would including a useful message (the optional third parameter).
edited 16 mins ago
answered 6 hours ago
Adrian Larson♦Adrian Larson
106k19113240
106k19113240
add a comment |
add a comment |
To clarify the syntax (if hits wasn't static):
count.hits = 1; // calls the "set"
Integer foo = count.hits; // calls the "get"
You can't call count.getHits()
. The calling syntax looks just like accessing a field.
However, since "hits" is static, it is not tied to an instance of your class, but to your class directly. You would access it as Counter.hits.
Then, you'll need to do something about visibility; either annotate with TestVisible, or make the property public.
New contributor
add a comment |
To clarify the syntax (if hits wasn't static):
count.hits = 1; // calls the "set"
Integer foo = count.hits; // calls the "get"
You can't call count.getHits()
. The calling syntax looks just like accessing a field.
However, since "hits" is static, it is not tied to an instance of your class, but to your class directly. You would access it as Counter.hits.
Then, you'll need to do something about visibility; either annotate with TestVisible, or make the property public.
New contributor
add a comment |
To clarify the syntax (if hits wasn't static):
count.hits = 1; // calls the "set"
Integer foo = count.hits; // calls the "get"
You can't call count.getHits()
. The calling syntax looks just like accessing a field.
However, since "hits" is static, it is not tied to an instance of your class, but to your class directly. You would access it as Counter.hits.
Then, you'll need to do something about visibility; either annotate with TestVisible, or make the property public.
New contributor
To clarify the syntax (if hits wasn't static):
count.hits = 1; // calls the "set"
Integer foo = count.hits; // calls the "get"
You can't call count.getHits()
. The calling syntax looks just like accessing a field.
However, since "hits" is static, it is not tied to an instance of your class, but to your class directly. You would access it as Counter.hits.
Then, you'll need to do something about visibility; either annotate with TestVisible, or make the property public.
New contributor
edited 42 mins ago
Mark Pond
18.2k13285
18.2k13285
New contributor
answered 1 hour ago
Mike LockettMike Lockett
112
112
New contributor
New contributor
add a comment |
add a comment |
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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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%2f247007%2fhow-to-access-class-members-decorated-using-get-set%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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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
Related: salesforce.stackexchange.com/q/157006/660 - also as noted below, your class properties must be visible outside of the class itself (public, global, or @TestVisible).
– Mark Pond
34 mins ago