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;
}







3















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?









share































    3















    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?









    share



























      3












      3








      3








      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?









      share
















      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





      share














      share












      share



      share








      edited 5 hours ago







      WEFX

















      asked 5 hours ago









      WEFXWEFX

      1588




      1588






















          1 Answer
          1






          active

          oldest

          votes


















          6














          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');
          }
          }




          share































            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            6














            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');
            }
            }




            share




























              6














              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');
              }
              }




              share


























                6












                6








                6







                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');
                }
                }




                share













                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');
                }
                }





                share











                share


                share










                answered 4 hours ago









                Adrian LarsonAdrian Larson

                111k19121259




                111k19121259















                    Popular posts from this blog

                    How did Captain America manage to do this?

                    迪纳利

                    南乌拉尔铁路局