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
34 votes
1 answer
45k 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 ...
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
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
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,540
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
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
0 votes
2 answers
7k views

how to cover the test class for apexpages.addmessage

I have written a test class that covers 86% leaving out the apexpages.addmessage () in catch block, but majorly it throws the validation exceptions on that object when I try to update a record.(Here I ...
vamshi krishna's user avatar
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
-1 votes
1 answer
2k views

Code coverage for wrapper class is 0% [closed]

I have written below Wrapper class. global class BI_VA_AutomationWrapper { global class BI_VA_AutomationRequest { webservice String userName; webservice String emailId; webservice ...
Chakrapani Reddy P's user avatar
-3 votes
1 answer
5k views

How to write a test class for dynamically running multiple Callouts [closed]

This is my apex class, please help me in writing a test class for it.Thanks global clas LoginClass{ public string Cookie_M; public string Session_M; public string Token_M; public string Username_M; ...
mira's user avatar
  • 47
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
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
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
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
4 votes
3 answers
14k views

Is the 75% code coverage requirement to deploy to production per class or overall?

My developer org's overall code coverage is 76%, but some classes are still 0% while others are 100%. Will I be able to deploy all my code or does Salesforce require a 75% coverage per class?
Adam 's user avatar
  • 1,426
1 vote
4 answers
640 views

Code Coverage 0% for trigger

I have written a trigger on custom object Order_Item__c. This object has a custom field Shipping_Number__c. When the user enters value in Shipping_Number__c and clicks save, the contact lookup field ...
SFDC_BigDog's user avatar
1 vote
1 answer
4k views

Testing a class that has multiple callouts?

What is the best practice for writing apex test classes where you have multiple callouts? My Test Class would then be testing the secondCallout method which happens to also call a firstCallout method ...
jojopogi's user avatar
  • 484
0 votes
0 answers
21 views

Cover aggregate Result Wrapper Class in test class [duplicate]

I'm having a hard tome covering the wrapper class for an aggregation in a test class. I'm only getting 48%. Can anyone see what I am missing? This Apex supports a VF page, where the user selects the ...
Newbie's user avatar
  • 1
0 votes
2 answers
1k views

Test Class - Error - Constructor not defined

Error - Constructor not defined: [PrepareSandboxPostCopy_Batch].() Test Class @isTest private class SandboxPostRefreshTestCase { static Testmethod void testSandboxPostCopyScript() { ...
Lokesh Malviya's user avatar
0 votes
2 answers
639 views

Test run for Web Service Callout batch class

I have an apex class which fetches and upserts data from external source to Salesforce. Below is the batch class: global class PolicyCalloutBatchClass implements Database.Batchable<Integer>,...
SFDCUser's user avatar
-1 votes
1 answer
489 views

Why can't I get code coverage for my trigger, even though I wrote a complete unit test? [duplicate]

I need help to create a test class for my trigger below: trigger UpdateLeadLastMQLDate on Event (before update) { Map<Id, Lead> leads = new Map<Id, Lead>(); for(Event record: ...
Adventurer's user avatar
-3 votes
1 answer
409 views

Not able to get the expected Assert statement in my test Class

My Assert Statement is failing in the below logic and i am not able to get the proper expected value , Please find the Codes in the Screenshots. Logic: public static void wmgRemoveOldGroupQueue(...
Abhijeet's user avatar
-4 votes
2 answers
2k views

Help with Code coverage, Loop and if conditions

I'm unable to have code coverage. Wrote a test class that has 24% coverage. How should I test loops and conditional statements? Following is my Apex Class... public class OrderTrackingList{ public ...
MnZ's user avatar
  • 3,162
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
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
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
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
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
5 votes
2 answers
2k views

Can't see Apex Class Code coverage in packaging org using code coverage link

How can I check the cumulative line by line code coverage of an Apex class that is included in a managed package? I have a dev org where I create a managed package. It has already gone through a ...
Daniel Ballinger's user avatar
4 votes
1 answer
432 views

Unable to upload managed package due to incorrect test coverage

I'm trying to upload a beta version of a managed package I'm developing, but it fails saying the test coverage is only 74%. But when I estimate the test coverage I've 91% both from 'Apex Classes' and ...
BuddhiP's user avatar
  • 543
4 votes
0 answers
116 views

Perform a deployment and execute all tests with a flag to opt out of collecting code coverage

I'd like to perform a deployment and execute all tests with a flag to opt out of collecting code coverage. The goal is to speed up Apex test execution. I'm aware that we can do it manually by ...
Pablo Fischer's user avatar
4 votes
2 answers
5k views

How can I deploy with Ant and run tests but ignore code coverage?

My team sometimes has a release process that requires deploying multiple packages in stages. We want to make sure that the system remains stable at the end of each of these deployments by running a ...
Pablo Venturino's user avatar
4 votes
2 answers
2k views

How to mock System.isFuture()?

I am writing some tests and I have encounter a problem. In my main app I have the following logic: public static void main() { if (System.isFuture()) { System.debug('Is future'); // ...
Matej J's user avatar
  • 143
4 votes
1 answer
2k views

Code Coverage on Inner Class/Variable Declarations

When we use an inner class, fields will not be covered unless otherwise we declare them with {get;set;}(even though they are accessible from the test class) Also these lines are not get counted as to ...
highfive's user avatar
  • 6,241
4 votes
0 answers
45 views

Test Class is listed under Console Code Coverage section [duplicate]

I've noticed some unclear behaviors when writing Apex test class. Even though these are not blocking for my implementation, I'd like to know what is going on underneath. When using class variables ...
highfive's user avatar
  • 6,241
4 votes
1 answer
273 views

Code Coverage - SalesForce Bug

While trying to upload our package (as an ISV partner) I noticed the following readings: Upload failed due to 73% code coverage (of course... we need 75%) From the "Apex Classes" screen, after ...
Uri Lukach's user avatar
  • 1,264
3 votes
3 answers
164 views

Coverage for Catch Block (cant generate exception in try{}) give another solution

NOTE : This question is not duplicate.Please go through code once. Here is the situation - I am calling this method from my test class, and here My_obj__c records are getting created and added in ...
Salesforce Steps's user avatar
3 votes
1 answer
2k views

Code Coverage during sfdx force:mdapi:deploy

I am trying to do a sfdx force:mdapi:deploy to my production org. I am just migrating one class file, however when I do that I keep getting code coverage failures on custom classes that are already in ...
MarkC5000's user avatar
  • 141
3 votes
3 answers
5k views

Can't get code coverage over 75% when deploying to production [closed]

I have been struggling with this for 5 hours now and can't find a solution. I am trying to push my code from the sandbox to production but when I try to validate the deployment in the production ...
TonyK's user avatar
  • 31
3 votes
0 answers
278 views

Salesforce Connect Custom Adapter test class

What is the correct way to approach testing Salesforce Connect custom adapters? Perhaps there is a unified approach to testing all of its parts, but specifically what is the approach for testing the ...
moth's user avatar
  • 454
3 votes
1 answer
5k views

Test Class throwing error "Method does not exist or incorrect signature"

Title says it all. I'm getting this error when trying to test a custom method. I checked to make sure that there weren't multiple classes with the same name as suggested by many other SE threads. I ...
Inagnikai's user avatar
3 votes
2 answers
2k views

Apex class won't save for lack of test coverage

I have been using Force.com IDE to work on VisualForce pages and classes directly in a live development environment. I was unaware of the requirement that 75% of your code had to be covered by tests. ...
JannieT's user avatar
  • 1,530
2 votes
1 answer
7k views

MIXED_DML_OPERATION, DML operation error when creating user role record in test class

I have a class wherein I am inserting two different records at the same time. These records are for a custom setting object named Setting__c and a custom object named Item__c. This works well and ...
curdual's user avatar
  • 315
2 votes
1 answer
654 views

Code Coverage Drop

My production org had code coverage of 85% but suddenly dropped to 55%. We have written test for every class and we are up to 63%. Anyone else have an idea of why this dramatic drop occurred and why ...
user5457's user avatar
2 votes
1 answer
2k views

Apex code coverage and static boolean recursion flags

I've written two Apex Triggers, one on the Opportunity object, and one on a custom object. These two triggers keep the two objects' checkboxes in sync; setting a value on one object's value sets the ...
snark17's user avatar
  • 247
2 votes
1 answer
3k views

Test Classes for Existing Salesforce Communities Controllers

I would like to increase my unit test coverage. I am using communities in our org. Salesforce comes with a bunch of existing controllers for communities: ChangePasswordController ...
Robs's user avatar
  • 9,330
2 votes
1 answer
307 views

System.asserts for testing Apex controller class with Current User

This could be pretty straight forward, but I am not sure what arguments I need to pass for comparison within System.asserts(I don't know if I really need to have asserts in my test method). Thing is, ...
Arnold Jr.'s user avatar
  • 1,521
2 votes
1 answer
258 views

Order of test while using <runTest>TestClass1</runTest>

Does SF run the tests in order in which the tests are specified in while running tests in a Deployment? Eg: <runTest>TestClass1</runTest> <runTest>TestClass2</...
Divyasshree's user avatar
2 votes
1 answer
124 views

Raise Code Coverage by replacing outdated code in Sandbox?

I have inherited an org with lots of outdated apex, and multiple changes to VRs, field requirements, field values, profile names, record type names etc. All of this was done without any insight into, ...
jshea's user avatar
  • 93
2 votes
1 answer
514 views

Test data returning Null

Test data creation: Account acct = new Account(Name='TestAccount', Field__c='TestInfo'); insert acct; Contact con = new Contact(FirstName = 'TestFname1',LastName = 'TestLname1', Email = 'test@...
Austin Evans's user avatar