Clearing or resetting a singleton in test class
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
I'm writing a test method to reset the value of a singleton variable, however I have no clue how to do it. please can someone assist me with the test code to test the following:
public class Service extends CompositeService
.....
//SINGLETON
private static Service singleton;
public static Service get()
if (singleton == null)
singleton = new Service ();
return singleton;
private Service ()
....
...
apex unit-test code-coverage
add a comment |Â
up vote
1
down vote
favorite
I'm writing a test method to reset the value of a singleton variable, however I have no clue how to do it. please can someone assist me with the test code to test the following:
public class Service extends CompositeService
.....
//SINGLETON
private static Service singleton;
public static Service get()
if (singleton == null)
singleton = new Service ();
return singleton;
private Service ()
....
...
apex unit-test code-coverage
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm writing a test method to reset the value of a singleton variable, however I have no clue how to do it. please can someone assist me with the test code to test the following:
public class Service extends CompositeService
.....
//SINGLETON
private static Service singleton;
public static Service get()
if (singleton == null)
singleton = new Service ();
return singleton;
private Service ()
....
...
apex unit-test code-coverage
I'm writing a test method to reset the value of a singleton variable, however I have no clue how to do it. please can someone assist me with the test code to test the following:
public class Service extends CompositeService
.....
//SINGLETON
private static Service singleton;
public static Service get()
if (singleton == null)
singleton = new Service ();
return singleton;
private Service ()
....
...
apex unit-test code-coverage
apex unit-test code-coverage
asked 40 mins ago
Minz
182
182
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
If your test class needs to "interfere" with private
variables that are not accessible through your class's public API, you should add the @TestVisible
annotation:
@TestVisible
private static Service singleton;
Your unit test can then do
Service.singleton = null;
when needed.
It's broadly preferable, where possible, to test using the public API, as this insulates your test class from the implementation details of your class and makes it run tests like any other consumer of the class's API. However, there are lots of situations, as it sounds like you've discovered, where it does become necessary to reach into the private state of a class during a test.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
If your test class needs to "interfere" with private
variables that are not accessible through your class's public API, you should add the @TestVisible
annotation:
@TestVisible
private static Service singleton;
Your unit test can then do
Service.singleton = null;
when needed.
It's broadly preferable, where possible, to test using the public API, as this insulates your test class from the implementation details of your class and makes it run tests like any other consumer of the class's API. However, there are lots of situations, as it sounds like you've discovered, where it does become necessary to reach into the private state of a class during a test.
add a comment |Â
up vote
3
down vote
If your test class needs to "interfere" with private
variables that are not accessible through your class's public API, you should add the @TestVisible
annotation:
@TestVisible
private static Service singleton;
Your unit test can then do
Service.singleton = null;
when needed.
It's broadly preferable, where possible, to test using the public API, as this insulates your test class from the implementation details of your class and makes it run tests like any other consumer of the class's API. However, there are lots of situations, as it sounds like you've discovered, where it does become necessary to reach into the private state of a class during a test.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
If your test class needs to "interfere" with private
variables that are not accessible through your class's public API, you should add the @TestVisible
annotation:
@TestVisible
private static Service singleton;
Your unit test can then do
Service.singleton = null;
when needed.
It's broadly preferable, where possible, to test using the public API, as this insulates your test class from the implementation details of your class and makes it run tests like any other consumer of the class's API. However, there are lots of situations, as it sounds like you've discovered, where it does become necessary to reach into the private state of a class during a test.
If your test class needs to "interfere" with private
variables that are not accessible through your class's public API, you should add the @TestVisible
annotation:
@TestVisible
private static Service singleton;
Your unit test can then do
Service.singleton = null;
when needed.
It's broadly preferable, where possible, to test using the public API, as this insulates your test class from the implementation details of your class and makes it run tests like any other consumer of the class's API. However, there are lots of situations, as it sounds like you've discovered, where it does become necessary to reach into the private state of a class during a test.
answered 35 mins ago


David Reed
22.9k41642
22.9k41642
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f237775%2fclearing-or-resetting-a-singleton-in-test-class%23new-answer', 'question_page');
);
Post as a guest
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
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
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