Is there a way to fake a method response using Mock or Stubs?
.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 5 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
|
|