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;








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 4 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 to create a command for the “strange m” symbol in latex? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)How do you make your own symbol when Detexify fails?Writing bold small caps with mathpazo packageplus-minus symbol with parenthesis around the minus signGreek character in Beamer document titleHow to create dashed right arrow over symbol?Currency symbol: Turkish LiraDouble prec as a single symbol?Plus Sign Too Big; How to Call adfbullet?Is there a TeX macro for three-legged pi?How do I get my integral-like symbol to align like the integral?How to selectively substitute a letter with another symbol representing the same letterHow do I generate a less than symbol and vertical bar that are the same height?

                    Българска екзархия Съдържание История | Български екзарси | Вижте също | Външни препратки | Литература | Бележки | НавигацияУстав за управлението на българската екзархия. Цариград, 1870Слово на Ловешкия митрополит Иларион при откриването на Българския народен събор в Цариград на 23. II. 1870 г.Българската правда и гръцката кривда. От С. М. (= Софийски Мелетий). Цариград, 1872Предстоятели на Българската екзархияПодмененият ВеликденИнформационна агенция „Фокус“Димитър Ризов. Българите в техните исторически, етнографически и политически граници (Атлас съдържащ 40 карти). Berlin, Königliche Hoflithographie, Hof-Buch- und -Steindruckerei Wilhelm Greve, 1917Report of the International Commission to Inquire into the Causes and Conduct of the Balkan Wars

                    Чепеларе Съдържание География | История | Население | Спортни и природни забележителности | Културни и исторически обекти | Религии | Обществени институции | Известни личности | Редовни събития | Галерия | Източници | Литература | Външни препратки | Навигация41°43′23.99″ с. ш. 24°41′09.99″ и. д. / 41.723333° с. ш. 24.686111° и. д.*ЧепелареЧепеларски Linux fest 2002Начало на Зимен сезон 2005/06Национални хайдушки празници „Капитан Петко Войвода“Град ЧепелареЧепеларе – народният ски курортbgrod.orgwww.terranatura.hit.bgСправка за населението на гр. Исперих, общ. Исперих, обл. РазградМузей на родопския карстМузей на спорта и скитеЧепеларебългарскибългарскианглийскитукИстория на градаСки писти в ЧепелареВремето в ЧепелареРадио и телевизия в ЧепелареЧепеларе мами с родопски чар и добри пистиЕвтин туризъм и снежни атракции в ЧепелареМестоположениеИнформация и снимки от музея на родопския карст3D панорами от ЧепелареЧепелареррр