Is there a way to fake a method response using Mock or Stubs? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern) 2019 Community Moderator Election ResultsDoes HttpCalloutMock work when the tested method doesn't return HttpResponse?Mock class not working with Dynamic & Unique end-pointHTTP Mock Response class has 0 coverageIs there a way to create more than 50000 records in a unit test context?Mock Service .setBody()Post callout behavior assertions for unit test with static resource callout mock failBasic Mock Test Coverage HelpHow can I reference a trigger's method and/or variable from a test class?Can we mock relationships in Apex?Method Is Not Visible: APEX Trailhead Unit Testing Challenge
Why did Europeans not widely domesticate foxes?
Specify the range of GridLines
Is a self contained air-bullet cartridge feasible?
Eigenvalues of the Laplacian of the directed De Bruijn graph
In search of the origins of term censor, I hit a dead end stuck with the greek term, to censor, λογοκρίνω
`FindRoot [ ]`::jsing: Encountered a singular Jacobian at a point...WHY
Why did Israel vote against lifting the American embargo on Cuba?
Why is water being consumed when my shutoff valve is closed?
When I export an AI 300x60 art board it saves with bigger dimensions
What's parked in Mil Moscow helicopter plant?
Determinant of a matrix with 2 equal rows
Simulate round-robin tournament draw
What do you call an IPA symbol that lacks a name (e.g. ɲ)?
Does a Draconic Bloodline sorcerer's doubled proficiency bonus for Charisma checks against dragons apply to all dragon types or only the chosen one?
Are there existing rules/lore for MTG planeswalkers?
Feather, the Redeemed and Dire Fleet Daredevil
SQL Server placement of master database files vs resource database files
Putting Ant-Man on house arrest
Was there ever a LEGO store in Miami International Airport?
Is it accepted to use working hours to read general interest books?
Why would the Overseers waste their stock of slaves on the Game?
What is the ongoing value of the Kanban board to the developers as opposed to management
Like totally amazing interchangeable sister outfit accessory swapping or whatever
How was Lagrange appointed professor of mathematics so early?
Is there a way to fake a method response using Mock or Stubs?
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern)
2019 Community Moderator Election ResultsDoes HttpCalloutMock work when the tested method doesn't return HttpResponse?Mock class not working with Dynamic & Unique end-pointHTTP Mock Response class has 0 coverageIs there a way to create more than 50000 records in a unit test context?Mock Service .setBody()Post callout behavior assertions for unit test with static resource callout mock failBasic Mock Test Coverage HelpHow can I reference a trigger's method and/or variable from a test class?Can we mock relationships in Apex?Method Is Not Visible: APEX Trailhead Unit Testing Challenge
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a helper method in my code called canQueryObject, and that method returns a boolean. I have some code that calls the method via the following line of code:
if(!myObjectHelper.canQueryObject(Schema.SObjectType.<<objectType>>)) ...
I'd like to test some scenarios where the scenario is false. Is there a way I can use Mock or Stubs in a unit-test to set canQueryObject to false?
unit-test mock
|
I have a helper method in my code called canQueryObject, and that method returns a boolean. I have some code that calls the method via the following line of code:
if(!myObjectHelper.canQueryObject(Schema.SObjectType.<<objectType>>)) ...
I'd like to test some scenarios where the scenario is false. Is there a way I can use Mock or Stubs in a unit-test to set canQueryObject to false?
unit-test mock
|
I have a helper method in my code called canQueryObject, and that method returns a boolean. I have some code that calls the method via the following line of code:
if(!myObjectHelper.canQueryObject(Schema.SObjectType.<<objectType>>)) ...
I'd like to test some scenarios where the scenario is false. Is there a way I can use Mock or Stubs in a unit-test to set canQueryObject to false?
unit-test mock
I have a helper method in my code called canQueryObject, and that method returns a boolean. I have some code that calls the method via the following line of code:
if(!myObjectHelper.canQueryObject(Schema.SObjectType.<<objectType>>)) ...
I'd like to test some scenarios where the scenario is false. Is there a way I can use Mock or Stubs in a unit-test to set canQueryObject to false?
unit-test mock
unit-test mock
edited 4 hours ago
WEFX
asked 5 hours ago
WEFXWEFX
1588
1588
|
|
1 Answer
1
active
oldest
votes
I typically use a fairly simple pattern and don't bring in a framework. It would look something like the following:
public virtual with sharing class MyObjectHelper
static MyObjectHelper instance = new MyObjectHelper();
@TestVisible static void setMock(MyObjectHelper mock) instance = mock;
public static Boolean canQueryObject(SObjectType sObjectType)
return instance.getCanQueryObject(sObjectType);
// the two method names cannot match or you will get a compile fail
protected virtual Boolean getCanQueryObject(SObjectType sObjectType)
return sObjectType.getDescribe().isQueryable();
Then in your test, you can have a mock return always true
or always false
, as you wish.
@IsTest
class MyObjectHelperTests
class HelperMock extends MyObjectHelper
Boolean isQueryable = true;
protected override Boolean getCanQueryObject(SObjectType sObjectType)
return isQueryable;
@IsTest static void testCannotQuery()
HelperMock mock = new HelperMock();
mock.isQueryable = false;
MyObjectHelper.setMock(mock);
Test.startTest();
Boolean canQuery = MyObjectHelper.canQueryObject(...);
Test.stopTest();
system.assertEquals(false, canQuery, 'Some informative message');
|
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I typically use a fairly simple pattern and don't bring in a framework. It would look something like the following:
public virtual with sharing class MyObjectHelper
static MyObjectHelper instance = new MyObjectHelper();
@TestVisible static void setMock(MyObjectHelper mock) instance = mock;
public static Boolean canQueryObject(SObjectType sObjectType)
return instance.getCanQueryObject(sObjectType);
// the two method names cannot match or you will get a compile fail
protected virtual Boolean getCanQueryObject(SObjectType sObjectType)
return sObjectType.getDescribe().isQueryable();
Then in your test, you can have a mock return always true
or always false
, as you wish.
@IsTest
class MyObjectHelperTests
class HelperMock extends MyObjectHelper
Boolean isQueryable = true;
protected override Boolean getCanQueryObject(SObjectType sObjectType)
return isQueryable;
@IsTest static void testCannotQuery()
HelperMock mock = new HelperMock();
mock.isQueryable = false;
MyObjectHelper.setMock(mock);
Test.startTest();
Boolean canQuery = MyObjectHelper.canQueryObject(...);
Test.stopTest();
system.assertEquals(false, canQuery, 'Some informative message');
|
I typically use a fairly simple pattern and don't bring in a framework. It would look something like the following:
public virtual with sharing class MyObjectHelper
static MyObjectHelper instance = new MyObjectHelper();
@TestVisible static void setMock(MyObjectHelper mock) instance = mock;
public static Boolean canQueryObject(SObjectType sObjectType)
return instance.getCanQueryObject(sObjectType);
// the two method names cannot match or you will get a compile fail
protected virtual Boolean getCanQueryObject(SObjectType sObjectType)
return sObjectType.getDescribe().isQueryable();
Then in your test, you can have a mock return always true
or always false
, as you wish.
@IsTest
class MyObjectHelperTests
class HelperMock extends MyObjectHelper
Boolean isQueryable = true;
protected override Boolean getCanQueryObject(SObjectType sObjectType)
return isQueryable;
@IsTest static void testCannotQuery()
HelperMock mock = new HelperMock();
mock.isQueryable = false;
MyObjectHelper.setMock(mock);
Test.startTest();
Boolean canQuery = MyObjectHelper.canQueryObject(...);
Test.stopTest();
system.assertEquals(false, canQuery, 'Some informative message');
|
I typically use a fairly simple pattern and don't bring in a framework. It would look something like the following:
public virtual with sharing class MyObjectHelper
static MyObjectHelper instance = new MyObjectHelper();
@TestVisible static void setMock(MyObjectHelper mock) instance = mock;
public static Boolean canQueryObject(SObjectType sObjectType)
return instance.getCanQueryObject(sObjectType);
// the two method names cannot match or you will get a compile fail
protected virtual Boolean getCanQueryObject(SObjectType sObjectType)
return sObjectType.getDescribe().isQueryable();
Then in your test, you can have a mock return always true
or always false
, as you wish.
@IsTest
class MyObjectHelperTests
class HelperMock extends MyObjectHelper
Boolean isQueryable = true;
protected override Boolean getCanQueryObject(SObjectType sObjectType)
return isQueryable;
@IsTest static void testCannotQuery()
HelperMock mock = new HelperMock();
mock.isQueryable = false;
MyObjectHelper.setMock(mock);
Test.startTest();
Boolean canQuery = MyObjectHelper.canQueryObject(...);
Test.stopTest();
system.assertEquals(false, canQuery, 'Some informative message');
I typically use a fairly simple pattern and don't bring in a framework. It would look something like the following:
public virtual with sharing class MyObjectHelper
static MyObjectHelper instance = new MyObjectHelper();
@TestVisible static void setMock(MyObjectHelper mock) instance = mock;
public static Boolean canQueryObject(SObjectType sObjectType)
return instance.getCanQueryObject(sObjectType);
// the two method names cannot match or you will get a compile fail
protected virtual Boolean getCanQueryObject(SObjectType sObjectType)
return sObjectType.getDescribe().isQueryable();
Then in your test, you can have a mock return always true
or always false
, as you wish.
@IsTest
class MyObjectHelperTests
class HelperMock extends MyObjectHelper
Boolean isQueryable = true;
protected override Boolean getCanQueryObject(SObjectType sObjectType)
return isQueryable;
@IsTest static void testCannotQuery()
HelperMock mock = new HelperMock();
mock.isQueryable = false;
MyObjectHelper.setMock(mock);
Test.startTest();
Boolean canQuery = MyObjectHelper.canQueryObject(...);
Test.stopTest();
system.assertEquals(false, canQuery, 'Some informative message');
answered 4 hours ago
Adrian Larson♦Adrian Larson
111k19121259
111k19121259
|
|