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
24
votes
1answer
7k 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 ...
36
votes
8answers
15k 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 ...
11
votes
7answers
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. ...
15
votes
8answers
11k 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 ...
10
votes
3answers
988 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 ...
-3
votes
1answer
2k 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; ...
0
votes
2answers
5k 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 ...
5
votes
3answers
1k 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 ...
32
votes
3answers
2k 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 ...
15
votes
2answers
7k 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 ...
12
votes
2answers
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 ...
3
votes
3answers
11k 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?
1
vote
4answers
481 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 ...
1
vote
2answers
358 views

Test Class for the Apex Class with try Catch block fails

I am trying to write a test class for the Apex Class with try - catch block and this is my first time coding test classes. The Apex Class is like below which has trycatch public class ...
0
votes
2answers
143 views

Test Class - Error - Constructor not defined

Error - Constructor not defined: [PrepareSandboxPostCopy_Batch].() Test Class @isTest private class SandboxPostRefreshTestCase { static Testmethod void testSandboxPostCopyScript() { ...
0
votes
0answers
40 views

Unit Test - No Code Coverage with Wrapper Class

Having issues getting any code coverage, no lines have been covered yet i am returning the data to the test method that I want and the System Assert is running as anticipated Class to Be Tested ...
-3
votes
1answer
100 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(...
-4
votes
2answers
1k 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 ...
25
votes
1answer
8k 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 ...
14
votes
1answer
933 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 ...
8
votes
3answers
6k 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 ...
1
vote
1answer
9k views

Creating Test Class for VF Page Controller

Having trouble creating a test class for my VF Page Controller I am trying to develop. I have created an inbound change set in my Production, and an outbound in my Sandbox. Now I am at the step of ...
7
votes
4answers
3k 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 ...
5
votes
2answers
3k 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-...
3
votes
0answers
112 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 ...
2
votes
3answers
125 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 ...
6
votes
1answer
3k 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 ...
5
votes
2answers
2k 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. ...
5
votes
2answers
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 ...
4
votes
0answers
41 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 ...
4
votes
1answer
363 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 ...
4
votes
2answers
3k 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 ...
4
votes
1answer
881 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 ...
4
votes
1answer
244 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 ...
4
votes
0answers
89 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 ...
3
votes
1answer
998 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 ...
3
votes
2answers
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. ...
3
votes
4answers
920 views

Referencing the RecordType Name instead of the ID in a test class

I have a test class which all works perfectly in the Sandbox however when i push to live it fails. On looking into it i have seen that my RecordTypeID is different in sandbox as it is in production.. ...
2
votes
1answer
457 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 ...
2
votes
1answer
207 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, ...
2
votes
1answer
98 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, ...
2
votes
3answers
3k 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 ...
2
votes
1answer
235 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@...
2
votes
1answer
1k 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 ...
1
vote
1answer
65 views

Test Code for Conditional Event Deletion is not running expected lines

I'm having some problems with a test class and I honestly can't figure out why. Maybe I've been staring at it for too long, but in any case - would appreciate if someone can tell me where I've gone ...
1
vote
2answers
3k views

How is Total code Coverage on ApexClass calculated

In developer Console all my classes are showing test coverage greater than 80%. But when I look at overall test coverage on ApexClasses page it shows at 74%. Any suggestions how is this calculated or ...
1
vote
2answers
669 views

Is there anyway to deactivate a trigger in Prod when the minumum code coverage is not met

I am not an apex developer and I am the only SFDC Admin at my company. We are a smaller non-profit org and the directive is to use standard "out of the box" functionality which I can do with my ...
1
vote
2answers
410 views

HTTP Mock Response class has 0 coverage

I have created a mock response to gain coverage for a hhtp request, as per the following documentation: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/...
1
vote
1answer
2k 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 ...
0
votes
0answers
229 views

Test Class for Dynamic SOQL [duplicate]

Pretty new with salesforce dev. So forgive me. I am doing a SOQL query and then updating records. The trick here is . I am grabbing my query from two custom fields which are in the Custom Settings . ...