Questions tagged [unit-test]

A unit test is code that is written and executed to test a specific portion/unit of source code. Unit tests are class methods with the testMethod modifier or @isTest annotation and are usually placed in separate test classes annotated with the @isTest annotation. Code that is executed by unit tests is considered covered. At least 75% of an organization's codebase must be covered by unit tests and every trigger must have at least 1% unit test coverage.

Filter by
Sorted by
Tagged with
18
votes
3answers
3k views

UNABLE_TO_LOCK_ROW Custom Setting Parallel Apex Testing Summer '13 (28.0) Regression

All of my classes, triggers, pages, components are set to API Version 28.0, Summer '13. None of my test classes/methods use SeeAllData=true annotation. Most of my test classes/methods insert identical ...
17
votes
1answer
9k views

HttpCalloutMock Interface where there is more than 1 callout

Here is some background- I have an apex future method which is being called from an trigger. The future method then performs 3 different http calls to an external system (same endpoints). All requests ...
17
votes
2answers
2k views

Test.setCreatedDate fails on note record created in test context

I'm trying to get a number of records inserted into the database sometime in the past, to allow for a cleanup batch to have proper test coverage. I can use Test.setCreatedDate on the account I'm ...
17
votes
4answers
2k views

Best Practice - Lightning JS (Code reuse, testing frameworks and IDE support)

I asked about the difference between controller and helper and it prompted me to ask another, more specific set of questions: What is the best way to reuse JavaScript code across Lightning Components?...
17
votes
3answers
33k views

Where can I read the debug output of test classes

Suppose I have a test class TestClassName. @isTest private class TestClassName { static testMethod void testMethodName() { System.debug('My debug statement'); System.assertEquals(1, 1)...
17
votes
4answers
428 views

Code Coverage in one big class vs multiple smaller classes

I'm having a very odd issue pop up wherein if I put all 75 of my account testMethods in one class, I get less than stellar coverage (maybe 70%) but if I split those testMethods up into 8-10 smaller ...
16
votes
8answers
6k views

How to get a list of Test Classes in an org

I need to get all test classes (classes with isTest Annotation) in an Org.. Is there anyway to filter test classes out of all the Apex Classes
16
votes
4answers
10k views

How can Document folder be created in unit tests?

Can Document folders be created in unit tests? If not, is there a simple workaround for testing Document objects in unit tests?
16
votes
2answers
4k views

How to mock a Database.saveresult?

We need to use database.insertImmediate(). It returns List<Database.SaveResult>. Now, we want to mock the database.insertImmediate() and not do the dml call and need to be able mock the ...
16
votes
4answers
12k views

How to test if outbound email is send

I have a batch that sends out an email notification to the triggering user when something went wrong. The code that does this is called from the finish() method. I'm now looking for an elegant way to ...
16
votes
3answers
6k views

Challenge with unit tests, mock callouts and running as a user

Background I am developing a number of business applications within a community. Many of these apps rely on callouts to another ERP system to verify or retrieve or push data. In order to adequately ...
16
votes
4answers
22k views

Too many SOQL queries: 101 issue with Unit Test Methods

We are facing couple of issue with Unit Test methods. SOQL queries limit hits before "Test.StartTest()". We want to test a batch process and it needs to creates records for around 8 to 10 objects ...
15
votes
5answers
22k views

How to test a scheduler which runs a batch?

I have a scheduler class which calls a batch Apex. I want to write an Apex test for the scheduler class. However, the test fails. It seems that the testing framework does not execute the batch class. ...
15
votes
8answers
13k 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 ...
15
votes
3answers
9k views

When is it appropriate to use (SeeAllData=true)

At this point I think we're painfully aware that using SeeAllData=true is one of the 7 Deadly Sins of Salesforce (assuming there's just 7). I know, for example, why we shouldn't use it: Can slow ...
15
votes
3answers
2k views

How can I speed up Developer Console?

Since Salesforce have moved their test execution to be asynchronous in Spring '13 we are forced to use the Developer Console to see Code Coverage for a single test class. My issue with this is that ...
15
votes
3answers
2k 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 ...
15
votes
5answers
19k views

Triggering a DML Exception for Test Coverage

I have an Apex Class, where 55% of the code is inside a catch, requiring a DML exception to be executed. Since the DML update request is using data from an SOQL request inside this class, I have no ...
15
votes
2answers
5k views

How do you write a unit test for a trigger whose only function is to make a callout?

What is considered a best practice for writing a unit test (to maintain code coverage) on a trigger whose sole function is making a callout (Which also means it has to use a future method)? Neither ...
15
votes
1answer
2k views

Testing a combination of webservice callouts and inserts

I have quite the conundrum and I'm not sure how to work around it. I have a batch method which it's entire purpose is to do a webservice callout and write to the database about what happened ...
15
votes
2answers
391 views

Salesforce Packaging Bug with Lightning Components? (Warning: very strange!)

Since years I'm using Metadata and Tooling Apex classes to connect with those APIs (based on Andrew Fawcetts work here https://github.com/afawcett/apex-toolingapi). Today I'm encountering a very ...
14
votes
4answers
17k views

How can Email Template be created in unit tests?

Can I create an Email Template as part of the unit test? If yes, how?
14
votes
2answers
12k views

NO_MASS_MAIL_PERMISSION Error from Unit Test on SingleMailMessage

I am currently working in a Config-Only sandbox. I have a unit test that is continually failing with the following error message: System.EmailException: SendEmail failed. First exception on row 0; ...
14
votes
3answers
3k views

What qualifies as running a test synchronously?

This Limits Documentation mentions: Total number of test classes that can be queued per a 24-hour period (5) - The greater of 500 or 10 multiplied by the number of test classes in the organization ...
14
votes
1answer
3k views

Uncommitted work pending in unit test with trigger and queueable callout

I have an after insert trigger that invokes queueable Apex which makes a callout. The trigger and callout work fine in the UI and I don't get any errors in normal operation. However, my unit test ...
14
votes
3answers
11k views

How to reduce the test class execution time while deploying?

I have optimized almost all my test classes but the "Run All Tests" execution time is still not reduced. I ran the tests in my full copy sandbox and it took 28 minutes. There are about 464 test ...
14
votes
1answer
13k views

How to Create Profile in test method?

I developed my code in my DE and created a custom profile named "XYZ". I used that profile in my Apex class and test methods. While installing that managed beta package in my sandbox my test method ...
14
votes
1answer
1k 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 ...
14
votes
1answer
22k views

The most sneaky way to increase your code coverage [closed]

I would like to share here this just for fun and as an example of what you should not do to pass the 75% of code coverage needed on SFDC. Maybe is not to much constructive but I think is funny to ...
13
votes
4answers
804 views

Is it worth it to try for 100% org coverage?

I saw an older post where someone had asked is it possible to get 100% code coverage, the responses were that it's real easy to get around 80% and then to cover the other 20% you're starting to get ...
13
votes
5answers
4k views

When testing can I not use existing Data?

I am currently programming a trigger on Opportunities. I created a test class for this and in my Sandbox I created an account, contact and opportunity. When I try to reference them Opportunity Opp ...
13
votes
3answers
25k views

Test class for schedulable interface

How can test this class? global class CustomerPortalRevokeSchedulable implements Schedulable { global void execute(SchedulableContext c) { CustomerPortalRevokeBatch theC = new ...
13
votes
2answers
3k views

@testSetup vs DataFactory?

When testing, how/when will @TestSetup annotation be preferable to a "DataFactory"? I understand how both works, but I don't see any case where I'd rather use the @testSetup annotation. I use this ...
13
votes
3answers
67k 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....
13
votes
1answer
2k views

How do I unit test code that includes a callout?

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 ...
13
votes
4answers
9k views

Ant Migration tool - sf:compileAndTest

I would like to configure the ant migration tool so that only test classes are run that we own so no managed package classes. Now i searched on the documentation of the tool and i only find the ...
13
votes
1answer
3k views

Clarification on Unit Testing a method that runs a SOSL query

I have a controller method that uses a SOSL query to generate a list of options. It works when used in the VF page, but the unit test is failing; logging shows the SOSL query is returning no results. ...
13
votes
2answers
3k views

Does Test.stopTest() ensure a System.Schedule/Database.Batchable completes in a test?

I am struggling to successfully test some code that is essentially the same as the code here Apex test class for schedulable class that checks for 5 batches running already. I can get the System....
13
votes
2answers
13k views

How to access custom settings in test class without using the seealldata?

I want to use a custom settings inside a test class, without using the SeeAllData=true What are the options I have? How can I accomplish it with a reasonable solution?
13
votes
1answer
4k views

How do I test asynchronous Apex?

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 ...
13
votes
2answers
6k views

How can I get code coverage programatically through Apex?

I've looked at the Automated Unit test Execution Recipe and see that it will email the results of running unit tests. I do not see anything in it that gives code coverage results, though. I'd like ...
13
votes
2answers
610 views

Any way to get consistent test counts when parallel testing is used?

Moving to parallel testing has made one of our builds run 4x faster but the test result files returned from: sfdx force:apex:test:report after polling via: sfdx force:apex:test:run vary on every ...
13
votes
4answers
18k views

FIELD_CUSTOM_VALIDATION_EXCEPTION during Unit Test

I have a validation rule, such that setting a Case escalation status to "approved" can only be done via particular users and/or emails via the automated email parser. I have a Unit Test that accounts ...
13
votes
2answers
973 views

Internal Server Error Accessing Org-Default Custom Setting In Test Method - Problem With Currency Values

This is driving me slightly crazy. I have a hierarchy type custom setting which has a currency field. Inside a trigger I do the following: Global_Settings__c gs = Global_Settings__c.getOrgDefaults();...
12
votes
3answers
22k views

How to create mock class for multiple Callouts in single class

I am facing a issue with my mock class where I am suppose to create a mock class for a class that makes several callouts in a single method based on criteria's. Now , when I am trying to create a mock ...
12
votes
5answers
4k views

What are the advantage of the @testSetUp annotation

Before Salesforce introduced @testSetup annotation in 2015, I used to create common data in a method and call it in every test method. Ex: @isTest private class clsAccountTest { private static ...
12
votes
3answers
20k views

Can we use try catch inside a test class? Is that a best practice?

Though i have not tried a test class with try catch blocks of code, i often saw people wrote try catch block inside a test class. I need expert advise as doing so is a best practice or conventional. ...
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 ...
12
votes
1answer
4k views

Testing Batch Job with HTTP Callouts

Suppose I have the following: global class CMBatchRegisterUsers implements Database.Batchable<sObject>, Database.AllowsCallouts, Database.Stateful { global final String orgID = UserInfo....
12
votes
1answer
3k views

Testing scheduled apex containing @future method?

I've seen plenty of posts and documentation on testing asynchronous apex in general, but what about code with multiple layers of asynchronous code? Specifically, I have a scheduled apex class which ...

1
2
3 4 5
88