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 Larson♦Adrian Larson

                111k19121259




                111k19121259













                    Popular posts from this blog

                    The Calvary Singular or Plural The 2019 Stack Overflow Developer Survey Results Are InAre collective nouns always plural, or are certain ones singular?Is “audience” singular or plural?“Wasn't” vs. “weren't” in a vernacular sentence“My last couple of years” — singular or plural?Is 'rest' singular or plural?Is “all but one” singular or plural?Whether to use the singular or plural form of basis?Singular and Plural for numbersIs there a plural form of teeth?performance: plural vs singular?singular or plural nouns?Singular and Plural

                    Is it possible to static_assert that a lambda is not generic?Can we get the type of a lambda argument?Is it possible to write a template to check for a function's existence?What is a lambda expression in C++11?Compiling with gcc fails if using lambda function for QObject::connect()c++11 - getting result_of, decltype, std::function and variadic templates working togetherTemplate Type Deduction with LambdasVariadic template method and std::function - compilation errorRecursively visiting an `std::variant` using lambdas and fixed-point combinatorsClang can't find template binary operator in fold expressionGenerating lambda from class templateGetting active value in std::visit without knowing which value is active

                    What is the correct present continuous form of “thunder” and “lightning”? Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar ManaraWhat is the past tense of “lightning”?What part of speech is the “be + verb” here? What tense are these sentences in?Present perfect and present perfect continuous for actions in progressIs it correct to mix past, present and future tense in a sentence?Correct verb form in two sentencesSimple vs Continuous in the Past Tense and in the Future TenseUsing present continuous passive (is being) for describing repeating (undesirable) actionssituations in which simple present and present continuous are interchangeable?Present Perfect Simple vs Present Perfect ContinuousWhat is the proper way to express a future action that depends on a past event?Progressive Form of Be and Lately in Present Continuous