Questions tagged [code-coverage]

Questions about specific lines of code that need coverage or platform bugs. 75% of your Apex code must be covered by test classes. This tag is not intended for use when code coverage is simply lacking, but for scenarios where because of a particular configuration or system issue code coverage is challenging.

Filter by
Sorted by
Tagged with
37 votes
6 answers
18k views

Apex Code Coverage: Test classes are included with 0% in overall code coverage calculation

We are making heavy use of stub and mock objects in our Apex tests. Most of the time we are defining the mocks as inner classes of our test classes. We have noticed, that test classes, which do ...
Alexander Johannes's user avatar
34 votes
1 answer
46k views

How do I increase my code coverage, or why can't I cover these lines?

This is a canonical question and answer developed by the community to help address common questions. If you've been directed here, or your question has been closed as a duplicate, please look through ...
33 votes
2 answers
23k views

Visual representation code coverage in VSCode

As per answer in this post, in order to get code coverage after running test classes from VSCode, we just need to add "-c" for the sfdx test command as below: sfdx force:apex:test:run -c -u myOrg -r ...
Rodrigo's user avatar
  • 1,265
33 votes
3 answers
3k views

What to Javascript-test in Lightning components and what not

Let's be honest. As an Apex developer used to provide near-100% test coverage for Apex the same should be true for Lightning components. As we move more code from the server to the client more test ...
Robert Sösemann's user avatar
23 votes
5 answers
6k views

Code Coverage column for apex class missing

I used to view % of code coverage in [Code Coverage] column for apex classes, after I run the test class. Now the column is missing. I notice about one week ago. How can I get it back as it is so ...
kitokid's user avatar
  • 1,738
18 votes
2 answers
15k views

How do I get Apex code coverage statistics when using Salesforce DX & Visual Studio Code

I am using Salesforce DX & Visual Studio Code to develop against a sandbox environment. After running an Apex test from Visual Studio Code, how can I view the code coverage from within Visual ...
Robs's user avatar
  • 9,330
18 votes
3 answers
5k views

Any way to easily create unit tests for a wsdl2apex generated Apex class?

I've created a new Apex class representing a third-party SOAP web service, using the "Generate from WSDL" button. Now I'd like to deploy this class to my production environment, but I'm getting an ...
Travis's user avatar
  • 303
15 votes
8 answers
16k views

Code coverage on dev console

A quick question. We used to have a code coverage highlighted in dev console on which lines of the class are covered and not covered before. Has salesforce removed this option or do i need to ...
Prady's user avatar
  • 12.2k
15 votes
4 answers
103k views

How to set a Date in apex test class?

I have written a test class in which I just want to set one date field. How can I achieve this? I am not able to set the value using the following code: Account acc=new Account(); acc....
uma451's user avatar
  • 809
15 votes
4 answers
17k views

Test that a Platform Event was published?

We have custom object CustomObject__c and platform event Platform_Event__e. Whenever any of CustomObject__c records are modified, platform event should be published. We need to write test class to ...
Patlatus's user avatar
  • 16.4k
15 votes
2 answers
3k views

3rd Party Code Coverage is below 75%, is this bad? What to do?

We have a bunch of installed apps which are just killing our code coverage. We're now down to only 77%, and all of the classes which are below 75% are from installed apps. Has anybody else had this ...
Zoom_v's user avatar
  • 3,860
14 votes
1 answer
3k views

Documentation Incorrect? Is it possible to cover/deploy triggers on Platform Events?

I am about to roll out some code which uses Platform Events, and hitting a troublesome roadblock in testing. Is calling Test.stopTest() supposed to cause subscriber triggers to fire and subsequently ...
Adrian Larson's user avatar
  • 150k
12 votes
2 answers
4k views

Are inactive triggers counted against test coverage?

We are in process of deploying a project to PROD and found that test coverage is coming up less then 75%. There are some triggers we have merged into single trigger. Question is: whether the ...
byadav's user avatar
  • 431
11 votes
7 answers
2k views

Develop Console Code Coverage Issues (Winter 15' Release) - Production vs. All Test

Wondering if this is a bug or there is something about CC that just doesn't make sense to me. Below is screenshot after screenshot of example of red-lined code coverage that makes no sense. ...
Xtremefaith's user avatar
  • 4,550
10 votes
3 answers
3k views

Test Class Fraud - what's my responsibility? [closed]

In doing some work for a client, I discovered a class that has 1500 lines (out of 2500) consisting entirely of public static void AAA() { Decimal a = 0; a++;a++;a++;a++;a++;a++;a++;a++;...
Scott Williams's user avatar
10 votes
3 answers
1k views

Why do Methods marked @TestSetup in Classes marked @IsTest count against test coverage requirements?

Every test class on my org that has a TestSetup annotated method shows 0/n lines covered in the developer console (Where n is the number of lines in the annotated method). I thought maybe this was ...
Jesse Milburn's user avatar
10 votes
3 answers
3k views

What classes and triggers have 0% code coverage?

I know that Salesforce is trying to push the developer console. Hence they removed the ability to see the code coverage in the classes view. The problem is that when I open the developer console and ...
Jose Chama's user avatar
9 votes
1 answer
4k views

Apex class Code Coverage stuck at 0% (No coverage data) after running test class

I have two classes: ClassA.cls - was generated from a WSDL some time ago. Currently on API v22.0 and hasn't been changed recently. Test_ClassA.cls - calls a large number of constructors in ClassA to ...
Daniel Ballinger's user avatar
9 votes
2 answers
2k views

Visual Studio Code Apex class code coverage colors

I'm looking at the code coverage results from a recent run of all the tests in an org, and I'm not sure what all the colors represent. The green and red seem fairly self explanatory: Green - covered ...
Daniel Ballinger's user avatar
9 votes
5 answers
8k views

How to succesfully test AgentWork trigger?

I am using AgentWork trigger in my logic and I cannot achieve code coverage goal. Simple test method: Id cId = [SELECT Id From Case Where Subject = 'Case Subject'].Id; User u = [SELECT Id From User ...
dan's user avatar
  • 355
9 votes
1 answer
120 views

Is it possible to suppress the "is your code coverage, take a quick tour" dialog?

It seems like every time I visit the Apex Text Execution page, I get a yellow popup asking me to go on a tour of the apex test system. After taking the tour four or five times, I kind of got sick of ...
pfooti's user avatar
  • 191
8 votes
3 answers
11k views

How to test page messages in unit test?

I have an Apex controller which unsubscirbes users from email via an opt-out form. I have written a test class which tests the method (or atleast partially at the moment). I would like to know how I ...
Deployment Failure's user avatar
8 votes
2 answers
1k views

Code Coverage Calculation - Seems to be including code in test methods

According to the documentation: Test classes (classes that are annotated with @isTest) are excluded from the code coverage calculation. This exclusion applies to all test classes regardless of ...
Eric's user avatar
  • 54.1k
8 votes
4 answers
9k views

No code coverage information was found for test run 7070U00000r8Ny3. Set "salesforcedx-vscode-core.retrieve-test-code-coverage": true

In user setting.JSON I am using { "salesforcedx-vscode-apex.java.home":"C:\\Program Files\\Java\\jdk1.8.0_131", "salesforcedx-vscode-core.show-cli-success-msg": true, "salesforcedx-...
gs650x's user avatar
  • 2,650
8 votes
8 answers
8k views

Code Coverage report for all classes

Winter 14 release removed Code Coverage column from list of class and triggers. Did anyone found any (nice) workaround to check Code Coverage of ALL classes?
Artur Kępczyński's user avatar
8 votes
2 answers
760 views

Pulling Production Code above 75%

I am having an issue, I have been with a company now and the person who came in before has deployed a bunch of stuff with no tests and shot the code coverage into the ground. I need to remove a bunch ...
AlphaBravo's user avatar
8 votes
3 answers
7k views

Why do Apex Unit Test CLASSES sometimes FAIL when all the contained METHODS PASS?

For the past month or so, I've noticed that when I execute some (not all) unit tests from within the Developer Console, sometimes the Status for folders display as failing (red X) even if/when all ...
Brian Kessler's user avatar
8 votes
1 answer
6k views

How to write test class for chained enqueued jobs?

I have an Queueable class which enqueues second job and now i want to write test class for the Queueable class. How should I cover my code in the second enqueued class of my test class? As specified ...
sfdeveloper's user avatar
7 votes
4 answers
860 views

Better practice to analyse and quantify code coverage with OR conditions

Test Coverage: I have 100% test coverage for my code base. I believed that, it is sufficient enough to deploy my code in Production and it is trust worthy that there won't be any bugs. Issue: But ...
karthikselva's user avatar
  • 2,446
7 votes
3 answers
3k views

Testing an Apex batch method execute() exception incrementing NumberOfErrors in the finish() method

I have a batch job that runs over a SOQL query locator. Occasionally there will be an unhandled exception in the execute() method. Later on in the finish() method I check the corresponding ...
Daniel Ballinger's user avatar
7 votes
1 answer
3k views

Testing Platform Cache usage

When writing Apex test cases for code that utilizes the platform cache what are the important scenarios I need to cover? Additionally, how would I address those is the test methods? For instance, ...
Daniel Ballinger's user avatar
7 votes
2 answers
1k views

Deployment Issue

I validating my components in Production it throws an Deploy Error: Deployment Error:"Average test coverage across all Apex Classes and Triggers is 'X'%,at least 75% test coverage is required"....
Ramesh Somalagari's user avatar
7 votes
2 answers
334 views

Code Coverage isn't changing from 65%

I have about 50 classes and triggers with only 6 of them < 100%. My code coverage is almost always around 90%. I compiled, re-ran tests via the Apex Test Execution page, and then estimated my org's ...
fourq's user avatar
  • 2,111
7 votes
2 answers
2k views

What's with the Chatter Answers test code that's failing?

After enabling Chatter Answers, I now have a trigger called chatter_answers_question_escalation_to_case_trigger. The trigger fails test coverage at 35%. What can I do about this?
fourq's user avatar
  • 2,111
6 votes
2 answers
659 views

Is this test for callout wrong? How to make a bulk?

I wrote mock and test for callout, but I don't think it's right. I think I check wrong things with asserts and maybe call the same methods a few times, while have another ways to get 100%. Have I? I ...
MichaelLev19's user avatar
6 votes
3 answers
2k views

Is it possible to get 100% Apex code coverage all the time?

Is it possible to get 100% code coverage all the time by considering all cases such as positive/negative, bulk/single etc. Are there any cases which can't be covered by test classes? I often hear that ...
sfdc_king's user avatar
  • 183
6 votes
1 answer
3k views

Can we Have two Http Mock Callouts in a single test class method

In My Method making two callouts in a single transaction Get Access Token For that API --> GetAccessToken Execute Payment When i ran the test class getting following error 08:50:00:839 ...
SFDC_Beginner's user avatar
6 votes
2 answers
3k views

Test Classes are included in code coverage

I've looked at a few questions but couldn't find the responses that worked for me. I've attached a screenshot of my classes and all of their code coverage. Any class ending in test is a test class. ...
tjcinnamon's user avatar
  • 1,620
6 votes
2 answers
5k views

Process Builder causing Apex Test class failures - How to handle?

Hoping someone can help with the following issue I am currently experiencing. I have created a new process via the process builder that sets the Contact owner to be the same as the Account Owner on ...
Gillian O Leary's user avatar
6 votes
1 answer
6k views

Deploy from Sandbox to Production Org SFDX

I created an SFDX project with manifest in VSCODE, authorized my sandbox org and retrieved source from manifest on the default package.xml. At this point I had the base setup with classes, triggers, ...
MarkC5000's user avatar
  • 141
6 votes
2 answers
1k views

Commenting out unused class tells me I have 0% code

In trying to get better coverage, I want to retire unused classes that have low coverage. When I try to deploy a commented out version of the class, I get an error: Your organization's code ...
jaw999's user avatar
  • 1,307
6 votes
1 answer
4k views

Test Coverage fail on simple before insert & update apex trigger

Apex n00b here. This has been gnawing me up over past few weeks. I have this very simple before insert / update trigger on Opportunity that auto-selects the Price Book based on a dropdown value ...
miCRoSCoPiC_eaRthLinG's user avatar
5 votes
3 answers
5k views

How to cover method return statement in Apex Class?

I'm writing a test for a schedulable class and so far it's working fine but I can't get the return statement to be covered by the test class. I'm missing something here and can't see it. The final ...
Alejandro Flores's user avatar
5 votes
2 answers
27k views

How do I cover 'addError' in test class

My Requirement is I have to cover so.AddError() in the test class. Apex Class: if(accountToUpdate.Inactive__c== true && accountToUpdate.Inactive__c <> accountBeforeUpdate.Inactive__c) ...
SFDCDJ's user avatar
  • 1,834
5 votes
4 answers
3k views

Test Class Showing 100% Coverage but APEX Class Is Showing Coverage: None

I wrote my first test class and I am not sure if I am reading the coverage correctly. On the bottom right side, the Overall Code Coverage is at 100%. When I go to my controller, the code coverage drop ...
Olivia's user avatar
  • 3,451
5 votes
3 answers
2k views

How to mock ApexTestQueueItem, AsyncApexJob, and ApexTestResult for test coverage?

I'm currently working on a set of classes that do the following: A class that when instantiated and executed, queues all Test classes in Apex to run by inserting ApexTestQueueItems. I call this one ...
Brian Barbour's user avatar
5 votes
4 answers
2k views

Is there a tool to run all tests and generate a coverage report?

I want to run all unit tests on my org and download the code coverage report. The best for me is editable excel format. Is it somehow possible? Do you know any useful tools for apex unit tests to ...
Łukasz Kojzar's user avatar
5 votes
1 answer
4k views

In test class can see all org users in list

I have written one query in test class for fetching users,as per my understanding query should return the user records created in test class only,but its giving all user from salesforce org.is that ...
ajay bagdad's user avatar
5 votes
1 answer
1k views

Stumped! Sandbox code coverage 89%, no org data in test class, yet deploy error - average code coverage is 0%

I created a rather simple Apex class VF11_controller in sandbox, which I'm looking to deploy to production. This apex class has one dependency, a VF page, which I added too in the change dataset. ...
Rosh's user avatar
  • 441
5 votes
1 answer
302 views

Test coverage for a relative date method

Newbie coder here, so I could use some help with test coverage. I've got a class for creating test/training data in a sandbox. At the beginning of that class, I've got a couple of utility methods to ...
Michael Kolodner's user avatar

1
2 3 4 5
27