What is recommended way to unit test JSON Parser based on JSON.deserializeUntyped calls?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
5
down vote

favorite












We are doing a lot of JSON data parsing in our solution by own JSON parser implementation. I wonder how can I write unit tests for JSON parser. Is it good approach to load JSON data from static resources? How can I get JSON as string from static resource? Do I need to use StaticResourceCalloutMock when I only test JSON parser methods that simply get input as string and return SObject?










share|improve this question



























    up vote
    5
    down vote

    favorite












    We are doing a lot of JSON data parsing in our solution by own JSON parser implementation. I wonder how can I write unit tests for JSON parser. Is it good approach to load JSON data from static resources? How can I get JSON as string from static resource? Do I need to use StaticResourceCalloutMock when I only test JSON parser methods that simply get input as string and return SObject?










    share|improve this question























      up vote
      5
      down vote

      favorite









      up vote
      5
      down vote

      favorite











      We are doing a lot of JSON data parsing in our solution by own JSON parser implementation. I wonder how can I write unit tests for JSON parser. Is it good approach to load JSON data from static resources? How can I get JSON as string from static resource? Do I need to use StaticResourceCalloutMock when I only test JSON parser methods that simply get input as string and return SObject?










      share|improve this question













      We are doing a lot of JSON data parsing in our solution by own JSON parser implementation. I wonder how can I write unit tests for JSON parser. Is it good approach to load JSON data from static resources? How can I get JSON as string from static resource? Do I need to use StaticResourceCalloutMock when I only test JSON parser methods that simply get input as string and return SObject?







      unit-test json static-resources parser






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 26 at 8:05









      gpoluch

      1238




      1238




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          11
          down vote



          accepted










          If the JSON is reasonably small then I would build it in the unit tests. That allows you to generate various permutations and always have the values to hand to assert against.



          The best way to do that (when you don't have Apex classes generated by e.g. JSON2Apex) is by creating nested Map<String, Object>s and then applying JSON.serialize. This ensures correct escaping and avoids ugly and error-prone string quoting and concatenation. Here is an example:



          List<Map<String, Object>> types = new List<Map<String, Object>>
          new Map<String, Object>
          'name' => 'Test Note added to Claim',
          'sobType' => String.valueOf(Note.SObjectType),
          'polymorphicParentSobType' => String.valueOf(Claim__c.SObjectType)
          ,
          new Map<String, Object>
          'name' => 'Test Note Title changed',
          'sobType' => String.valueOf(Note.SObjectType),
          'sobField' => String.valueOf(Note.Title)

          ;
          String jsonString = JSON.serilize(types);


          At first sight, this looks pretty confusing, but it is just using a List for any JSON array [...] and a Map for any JSON object .... And Apex has good data initialization syntax available such as => for setting up map name-value pairs. You don't have to build it all at once either, you can set up pieces and then assemble them.



          If the JSON is very large and you have examples that you want to use in tests then static resources are one way to go. You can query static resources (in product code or test code) like this:



          Set<String> staticResourceNames = ...;
          for (StaticResource sr : [
          select Name, Body
          from StaticResource
          where Name in :staticResourceNames
          ])
          String jsonString = sr.Body.toString();
          ...






          share|improve this answer






















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "459"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f230142%2fwhat-is-recommended-way-to-unit-test-json-parser-based-on-json-deserializeuntype%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            11
            down vote



            accepted










            If the JSON is reasonably small then I would build it in the unit tests. That allows you to generate various permutations and always have the values to hand to assert against.



            The best way to do that (when you don't have Apex classes generated by e.g. JSON2Apex) is by creating nested Map<String, Object>s and then applying JSON.serialize. This ensures correct escaping and avoids ugly and error-prone string quoting and concatenation. Here is an example:



            List<Map<String, Object>> types = new List<Map<String, Object>>
            new Map<String, Object>
            'name' => 'Test Note added to Claim',
            'sobType' => String.valueOf(Note.SObjectType),
            'polymorphicParentSobType' => String.valueOf(Claim__c.SObjectType)
            ,
            new Map<String, Object>
            'name' => 'Test Note Title changed',
            'sobType' => String.valueOf(Note.SObjectType),
            'sobField' => String.valueOf(Note.Title)

            ;
            String jsonString = JSON.serilize(types);


            At first sight, this looks pretty confusing, but it is just using a List for any JSON array [...] and a Map for any JSON object .... And Apex has good data initialization syntax available such as => for setting up map name-value pairs. You don't have to build it all at once either, you can set up pieces and then assemble them.



            If the JSON is very large and you have examples that you want to use in tests then static resources are one way to go. You can query static resources (in product code or test code) like this:



            Set<String> staticResourceNames = ...;
            for (StaticResource sr : [
            select Name, Body
            from StaticResource
            where Name in :staticResourceNames
            ])
            String jsonString = sr.Body.toString();
            ...






            share|improve this answer


























              up vote
              11
              down vote



              accepted










              If the JSON is reasonably small then I would build it in the unit tests. That allows you to generate various permutations and always have the values to hand to assert against.



              The best way to do that (when you don't have Apex classes generated by e.g. JSON2Apex) is by creating nested Map<String, Object>s and then applying JSON.serialize. This ensures correct escaping and avoids ugly and error-prone string quoting and concatenation. Here is an example:



              List<Map<String, Object>> types = new List<Map<String, Object>>
              new Map<String, Object>
              'name' => 'Test Note added to Claim',
              'sobType' => String.valueOf(Note.SObjectType),
              'polymorphicParentSobType' => String.valueOf(Claim__c.SObjectType)
              ,
              new Map<String, Object>
              'name' => 'Test Note Title changed',
              'sobType' => String.valueOf(Note.SObjectType),
              'sobField' => String.valueOf(Note.Title)

              ;
              String jsonString = JSON.serilize(types);


              At first sight, this looks pretty confusing, but it is just using a List for any JSON array [...] and a Map for any JSON object .... And Apex has good data initialization syntax available such as => for setting up map name-value pairs. You don't have to build it all at once either, you can set up pieces and then assemble them.



              If the JSON is very large and you have examples that you want to use in tests then static resources are one way to go. You can query static resources (in product code or test code) like this:



              Set<String> staticResourceNames = ...;
              for (StaticResource sr : [
              select Name, Body
              from StaticResource
              where Name in :staticResourceNames
              ])
              String jsonString = sr.Body.toString();
              ...






              share|improve this answer
























                up vote
                11
                down vote



                accepted







                up vote
                11
                down vote



                accepted






                If the JSON is reasonably small then I would build it in the unit tests. That allows you to generate various permutations and always have the values to hand to assert against.



                The best way to do that (when you don't have Apex classes generated by e.g. JSON2Apex) is by creating nested Map<String, Object>s and then applying JSON.serialize. This ensures correct escaping and avoids ugly and error-prone string quoting and concatenation. Here is an example:



                List<Map<String, Object>> types = new List<Map<String, Object>>
                new Map<String, Object>
                'name' => 'Test Note added to Claim',
                'sobType' => String.valueOf(Note.SObjectType),
                'polymorphicParentSobType' => String.valueOf(Claim__c.SObjectType)
                ,
                new Map<String, Object>
                'name' => 'Test Note Title changed',
                'sobType' => String.valueOf(Note.SObjectType),
                'sobField' => String.valueOf(Note.Title)

                ;
                String jsonString = JSON.serilize(types);


                At first sight, this looks pretty confusing, but it is just using a List for any JSON array [...] and a Map for any JSON object .... And Apex has good data initialization syntax available such as => for setting up map name-value pairs. You don't have to build it all at once either, you can set up pieces and then assemble them.



                If the JSON is very large and you have examples that you want to use in tests then static resources are one way to go. You can query static resources (in product code or test code) like this:



                Set<String> staticResourceNames = ...;
                for (StaticResource sr : [
                select Name, Body
                from StaticResource
                where Name in :staticResourceNames
                ])
                String jsonString = sr.Body.toString();
                ...






                share|improve this answer














                If the JSON is reasonably small then I would build it in the unit tests. That allows you to generate various permutations and always have the values to hand to assert against.



                The best way to do that (when you don't have Apex classes generated by e.g. JSON2Apex) is by creating nested Map<String, Object>s and then applying JSON.serialize. This ensures correct escaping and avoids ugly and error-prone string quoting and concatenation. Here is an example:



                List<Map<String, Object>> types = new List<Map<String, Object>>
                new Map<String, Object>
                'name' => 'Test Note added to Claim',
                'sobType' => String.valueOf(Note.SObjectType),
                'polymorphicParentSobType' => String.valueOf(Claim__c.SObjectType)
                ,
                new Map<String, Object>
                'name' => 'Test Note Title changed',
                'sobType' => String.valueOf(Note.SObjectType),
                'sobField' => String.valueOf(Note.Title)

                ;
                String jsonString = JSON.serilize(types);


                At first sight, this looks pretty confusing, but it is just using a List for any JSON array [...] and a Map for any JSON object .... And Apex has good data initialization syntax available such as => for setting up map name-value pairs. You don't have to build it all at once either, you can set up pieces and then assemble them.



                If the JSON is very large and you have examples that you want to use in tests then static resources are one way to go. You can query static resources (in product code or test code) like this:



                Set<String> staticResourceNames = ...;
                for (StaticResource sr : [
                select Name, Body
                from StaticResource
                where Name in :staticResourceNames
                ])
                String jsonString = sr.Body.toString();
                ...







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Aug 26 at 11:31

























                answered Aug 26 at 9:12









                Keith C

                91.2k1084189




                91.2k1084189



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f230142%2fwhat-is-recommended-way-to-unit-test-json-parser-based-on-json-deserializeuntype%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?