2

Can someone clarify what Salesforce means by 'code coverage'?

I'm trying to deploy a change set that includes an Apex Trigger, Apex Class and Apex Test Class. The trigger is sending an external request to another server, so I've wrapped the Class code that sends the request within a Test.isTestRunning() conditional. This validates (and works) in the sandbox, but when I deploy to our production organization, it fails because the code coverage is less than 75%.

When I look at the documentation, I see that there's a way to display color coded lines of which code is covered and which lines are not, but the only selection I have in the developer console dropdown (top left) is 'Code Coverage: None'

Appreciate any help!

4
  • are you getting error during deployment? Sep 28, 2016 at 18:43
  • are you preparing any data dependent to org? Sep 28, 2016 at 18:44
  • in the sandbox, open the dev console with your trigger/class. It'll say no coverage. Open the test class and click run test. It'll go through and then when you click back to the trigger/class it should give you coverage for the test class if you have any. Also, I think you only need 1% on the trigger/class to deploy the component to Prod, where you get issues is the overall across all triggers and classes has to be above 75% so if this code is low it will bring the overall below and stop you. Sep 28, 2016 at 18:45
  • Before I attempt to answer... Do you know what "Test Classes" are and what they do? If you do, then read the answers below. If you don't, I can attempt to answer that instead. Sep 28, 2016 at 19:00

2 Answers 2

1

While your question is about how to view code coverage in the developer console, the underlying issue preventing your deployment is pretty clear.

I don't need to see your code, or know which lines are covered to say that the issue preventing your deployment is that you've wrapped your callout code in an if block that prevents it from running when you're running unit tests. (Thanks for including that information, by the way. It was very helpful in determining the root cause of the issue that led you to ask this question).

It is true that we can't make callouts during a unit test, but wrapping a good portion of your callout code (the code which sends the request to the external system, and handles the response) in an if(!Test.isRunningTest()) isn't really the right way to go about this.

Code that isn't run during tests is not covered.

To get coverage for your callout code, you'll need to mock the callout by creating another class that implements the HTTPCalloutMock interface. What this does is it intercepts the callout request in your test, and returns an HttpResponse instance that you define.

You can add the @isTest annotation to the class that implements HttpCalloutMock so that the mock implemenation doesn't require coverage itself.

I haven't tested this myself, but you may be able to simply use an inner class, defined inside of your existing test class, to keep things a little more tidy. An example would be

@isTest
public class myTestClass{
    public class testMock implements HttpCalloutMock{
         public HttpResponse respond(HttpRequest req){
             // define your pre-programmed response here
         }
    }

    static testMethod void myTest(){
        Test.setMock(HttpCalloutMock.class, new testMock());

        // rest of your test
    }
}
3
  • Thanks @Derek F - very helpful - the only issue that I'm running into is running the test on an@future (async) Http request - my understanding is that the @future method must return void, how do I set up the test to accept the void return?
    – dbcn
    Sep 28, 2016 at 20:03
  • @NathanBennett You are correct, @future methods must return void. I'm not 100% sure about what issues you're having testing the call through an @future method. Can you elaborate some more? Besides getting coverage by simply calling the @future method, assertions need to be done against the side effects (like updating fields on an sObject) of running the @future method rather than a return value.
    – Derek F
    Sep 28, 2016 at 20:44
  • thx @Derek F - the outbound ajax call is based on user interaction through the UI (sObject update) - so I'm not sure what I'd be able to test against, since the value would already have been changed before the request was sent...
    – dbcn
    Sep 29, 2016 at 12:50
1

Code coverage : Before you can deploy your code or package it for the Force.com​ AppExchange, at least 75% of Apex code must be covered by tests, and all those tests must pass. In addition, each trigger must have some coverage so with this definition if you are seeing code coverage none there can be two possible reason

  1. You have recently modified your trigger code which is setting your code coverage for that class to 0.
  2. Your test class is failing.

So to resolve these issue. Run all test from apex class page and once you see all test completion status in Apex Test Execution menu option come back again on following screen and verify your org code coverage.

enter image description here

but if you actually not seeing code covered it means you have some issue with your test class which is not executing your trigger and failing in some way. In that case check debug log for exact issue.

Even though code coverage is a requirement for deployment, don’t write tests only to meet this requirement. Make sure to test the common use cases in your app, including positive and negative test cases, and bulk and single-record processing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.