<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shailendra Modi on Medium]]></title>
        <description><![CDATA[Stories by Shailendra Modi on Medium]]></description>
        <link>https://medium.com/@shailendra_60588?source=rss-35dd967a61e7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*kYHJ1JHfHCWsF52jihNP_g.jpeg</url>
            <title>Stories by Shailendra Modi on Medium</title>
            <link>https://medium.com/@shailendra_60588?source=rss-35dd967a61e7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 11:27:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shailendra_60588/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Salesforce Apex Test Class with the understanding & usage of Test.isRunningTest()]]></title>
            <link>https://medium.com/@shailendra_60588/salesforce-apex-test-class-with-the-understanding-usage-of-test-isrunningtest-43644210acb5?source=rss-35dd967a61e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/43644210acb5</guid>
            <category><![CDATA[apex-batch-class]]></category>
            <category><![CDATA[isrunningtest]]></category>
            <category><![CDATA[salesforce]]></category>
            <category><![CDATA[salesforce-unit-testing]]></category>
            <category><![CDATA[apex-test-class]]></category>
            <dc:creator><![CDATA[Shailendra Modi]]></dc:creator>
            <pubDate>Wed, 11 Mar 2020 09:21:31 GMT</pubDate>
            <atom:updated>2020-03-11T11:16:55.112Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Apex Test Class with the usage of Test.isRunningTest() Methods in Apex Class-</strong></p><p><strong>Salesforce Apex Test Class: </strong>For any organization, there is a demand for quality software products &amp; tested software that should work fine. It should be working functionality-wise with bug-free.</p><p>In Salesforce, testing is the integrated part of Apex or any application development. In Salesforce force.com, there is Apex Testing Framework that enables you to write &amp; execute tests for Apex classes, Scheduler, Batch Classes &amp; Trigger, etc. Apex unit testing for class &amp; trigger is done through the Apex Test classes which ensure high quality for Apex Code. Apex Code can be written in the sandbox or developer org but not in the production org.</p><p>In salesforce, the code must have at least 75% code coverage with including positive &amp; negative test cases as well in order to be deployed into production org.</p><p><strong>Test.isRunningTest(): </strong>The Test.isRunningTest() method in the Apex class, batch or trigger code is used to identify the pieces of code being executed &amp; invoked from a Test class execution. The Test.isRunningTest() method is used to define whether the code is executed by the Apex test class.</p><p><strong>What is the use of the Test.isRunningTest() method in the Apex Class?</strong></p><p>Test.isRunningTest() method usually comes in handy, when you intend to conditionally restrict execution of certain Apex code blocks, based on whether they are being invoked from a test class or not.</p><p><strong>Challenge</strong></p><p>Recently I worked for a client where we were using a managed package. The managed package comes with declarative options like quick action/lightning web components etc but also it comes with a batch class that can be used by developers in existing Apex code. This batch class eventually calls web service. The challenge was code coverage of such web service which is being invoked from the managed package.</p><p>As usual, I could think of</p><p>global class YourHttpCalloutMockImpl implements HttpCalloutMock {</p><p>global HTTPResponse response(HTTPRequest req) {</p><p>// Create a fake response</p><p>// Set response values, and</p><p>// return response</p><p>}</p><p>}</p><p>Now for this, I had to do the following:</p><ul><li>Create a fake response</li><li>Set response values</li><li>And return the response</li></ul><p>For the client’s projects, I had done this so many times, almost in all projects. But I was unsure of the challenge I was about to face that too in a strict timeline.</p><p>I thought let’s create a fake response. But how would I know what response to be prepared because the web service is being called from the managed package class? FYI we cannot see the implementation of the class in the managed package. We either see (hidden) or unimplemented methods with their signatures only, it is because the code is someone’s intellectual property and should be protected.</p><p><strong>Solutions that didn’t work</strong></p><p>#1 Anyhow I managed to explore components of managed packages via Setup -&gt; Installed Packages. I could see signatures of methods in a batch class which shows the parameters of methods and return value type.</p><p>But it wasn’t helping me out because I wanted to know the exact implementation of the method!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*dop40v40JIfna3wY" /></figure><p>#2 Is callout is written in BulkMemberSearch()? Yes, but I can’t see the code. I was thinking managed package developers must have written some kind mock response generated and here is what I found.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*D-IU6Ec_DnfoquvR" /></figure><p>I thought I am done now and I can use <strong><em>MockHttpResponseGenerator</em> </strong>in my Test. Mock API something like this</p><p><em>Test.setMock(HttpCalloutMock.class, new </em><strong><em>MockHttpResponseGenerator</em></strong><em>());</em></p><p>I tried this and got an error “Invalid type: MockHttpResponseGenerator”. That’s obvious as we can not use managed package class in our code until it is exposed to be used in that way.</p><p>#3<strong> </strong>Now what? I could think of creating my own MockHttpResponseGenerator by guessing the response and by analyzing the debug log I couldn’t find anything about the response. It didn’t work again!</p><p><strong>Very close to the solution</strong></p><p>#4 I knew something that performing web service callouts in Apex are not supported within Test Code. Hence you could use the <strong>Test.isRunningTest()</strong> to conditionally identify and route the execution of a code block that calls the Test Mock framework to simulate a mock, callout response.</p><p>The Developers were using the batch provided by a managed package in one of the scheduler classes. I asked the developer to wrap their code into if condition.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/0*S12VLr6Fi0qOmlFy" /></figure><p>Also, I created the Apex test class for the above Apex scheduler for unit testing purposes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/0*OQ_EhIl4qyl2v7eq" /></figure><p>Yes, test class could run successfully with no web service error, but I found code coverage only 20% instead of more than 75%.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/0*HsEQWNFmZoxv6_lt" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/0*hhauX9sBMRnwsp5s" /></figure><p>Now, the problem was only with the test class code coverage. Here, I reviewed my test class code &amp; Apex scheduler code from their batch class executed. I needed to cover the test class for more than 75%, so I did focus only on increasing the code coverage with all possible positive &amp; negative test cases. I found the solution again after reviewing the scheduler class that I should be able to write the only one line of code inside of <strong>IF(!Test.isRunningTest()){} </strong>instead of the piece of code because my batch class was affected only from the only line of code where the batch class was executed.</p><p>This statement <strong>IF(!Test.isRunningTest()){} </strong>can block &amp; restrict only one line of code that was not covered by test class &amp; other pieces of code was running as usual. My test class was executed &amp; ran successfully. Even I found a test class with code coverage of more than 75%. After spending a lot of time &amp; trying to apply with different solutions here, finally, I could succeed with this solution in the Apex test class.</p><p><strong>Finally, this solution worked for me completely &amp; found code coverage of 80%. Below is the screenshot for the piece of code-</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/0*B0CB6oxOGnqxsMcm" /></figure><p><strong>Few More Facts</strong></p><ul><li>The 75% requirement is on all Apex code in your org’s namespace. Managed code has its own namespace, so it is not included.</li><li>When deploying to production, all tests in your org’s namespace will be run. Managed tests will not be run by default.</li><li>If the deployment package explicitly marks runAllTests to true, managed tests will also run. These will need to be executed without any failures should the flag be set. Despite this, the 75% requirement for code coverage is still only on unmanaged code.</li></ul><p><strong>Author Bio</strong></p><p>My name is Shailendra Modi and I work as a Software Test Engineer at Techcoopers powered by Selectiva Inc. which is a <a href="https://selectiva.com/">Custom Software Development Company</a> having major products- Adminified, Select*IP, SelectMeta, etc. I have worked on various technologies such as Salesforce &amp; Unit Testing in Salesforce, Junit, TestNG, Selenium Automation. And I have also worked on Javascript Testing Frameworks such as the most popular used automation frameworks- <strong>JEST</strong>, Jasmine &amp; Mocha. I believe in exploring and learning the latest technologies &amp; testing frameworks that help to optimize human efforts. I am always ready to help &amp; share my knowledge &amp; experience that I have.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=43644210acb5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>