Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
The results are in! See what nearly 90,000 developers picked as their most loved, dreaded, and desired coding languages and more in the 2019 Developer Survey.

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.

126
votes
8answers
17k views

Unit testing code which has logic around the CreatedDate

An example of the problem would be trying to test a trigger on a case which will fire on update, and look for time differences between when a case was created and now. Ideally in the unit tests I want ...
40
votes
4answers
1k views

Why Are Data Silos Important in Unit Tests?

I have a lot of strong opinions about this issue, but had a hard time finding official documents today to back up my position. A lot of times we say it is bad practice to use seeAllData=true, but we ...
38
votes
2answers
5k views

Test.loadData() - undocumented (but useful) behavior loading relationships

Premise I want to load Accounts and their related Contacts using Test.loadData() and StaticResources. How do I do this given that the Contact records must have the ID of their parent Account yet the ...
36
votes
8answers
4k views

Is there a way to setup continous integration for apex tests?

I would like to have continous integration (with Jenkins) for my apex code in order to run them automatically after each push to my git repository. Does anyone know a way how to do that?
33
votes
6answers
8k views

Is it possible to test apex that relies on field history tracking?

I have some code that relies on field history tracking by querying the History for a custom object. This works fine, but it appears that the history records aren't created during the test so none of ...
33
votes
8answers
12k 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 ...
30
votes
1answer
24k views

Writing Test Classes for Apex RestService

I was writing test classes for my apex Rest Service that i had built for an ipad app. I noted very interesting things while experimenting.Before anything i just did a google and found an interesting ...
30
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 ...
29
votes
3answers
8k views

Is there any way of confirming that an @future method has been called/queued in a test class?

I have a Controller (FooController) which calls an @future method (bar()) belonging to another class (FooServices). Ideally in my test class for this Controller (FooControllerTest) I only want to ...
28
votes
7answers
8k views

Use of @IsTest versus testMethod and location of test methods in classes

Is there any advantage to using the older testMethod keyword to identify test methods or is it better to use the @IsTest annotation? Does the location of these test methods have any significance? E.g....
28
votes
6answers
23k views

Testing HttpCallout with HttpCalloutMock and UnitTest Created Data

We have a callout to an external webservice that requires retrieving some data from the database via SOQL and then issuing an HTTP GET request, and performing various logic based upon the response. ...
26
votes
1answer
17k views

Error: MIXED_DML_OPERATION on setup and non-setup objects

In my test class i have the creation of a dummy custom setting and a dummy lead: Group g1 = new Group(Name='group name', type='Queue'); insert g1; QueuesObject q1 = new ...
26
votes
1answer
11k views

Why use Test.setCurrentPage() method in a unit test?

What is the purpose of the Test.setCurrentPage(..) method and why would I use it in my apex controller unit tests? According to the salesforce docs, https://www.salesforce.com/us/developer/docs/...
25
votes
3answers
19k views

How should I build test methods for Visualforce Controller Extensions

I would like to know the best way to write test methods for controller extension classes that are used by Visualforce. What is the minimum set code that I need to cover the basic operation of the ...
25
votes
5answers
8k views

“Could not run tests on class …”

Today, I am writing test code in my Sandbox, and until 15 minutes ago, my test execution was working. Now, all of a sudden, my tests stopped working. I can't run even a single test! Test execution ...
24
votes
2answers
8k views

How to check if any error has been added to a record?

I have created a method which is being called through a trigger. I am adding error to a record if it is a duplicate one using 'addError()' method . Now , I want to cover this method in my test class. ...
22
votes
8answers
4k views

Faster unit tests?

I'm running unit tests in Eclipse (Force.com IDE) and they are taking 5min+ to execute. Is there an alternative way to run them, or are there tips to speeding this up? I know I can run the tests from ...
21
votes
4answers
5k views

Unit tests that need more than one WebServiceMock

I'm dealing with an odd situation where I need to call Webservice A to authenticate to Webservice B and Webservice C, all with different endpoints. I know how to setup mocks to test B and C ...
21
votes
5answers
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 ...
21
votes
2answers
912 views

Is it possible to simulate record locks in test methods?

I would like my test cases to also test exception handling. In theory an easy way to cause an exception on DML is to lock a record that it'll update so that the update will fail. But I'm having ...
20
votes
5answers
7k views

Unit Test - Creating Custom Metadata

I have three questions that I'd like to be clarified. Is their anyway we could create Custom Metadata in Test Classes? I tried to create on my Test Class but I got an error saying the fields of the ...
20
votes
1answer
7k views

Test method to support getContent call

I have created getContent() method to send a pdf page in email using Visualforce page. But while running test method on the controller, I am getting following error message - "Methods defined as ...
20
votes
4answers
4k views

Ignoring Validation rules when deploying code

Scenario: Production org has sufficient code coverage with unit-tests. Someone adds validation rule(s) which cause the tests to break. Minimum 75% code coverage is no longer there. We come along to ...
20
votes
1answer
3k views

Can't get Test.loadData method to run

I cannot get past various system exceptions trying to use the loadData method. It seems to be something with the csv file; changing it alters the cause of failure. I am using data from production ...
19
votes
1answer
4k views

Creating unit tests without interacting with the database - creating fake IDs

I was at dreamforce this year and I saw a session on testing and have looked online everywhere to try to find this trick the guy did. He was creating sObjects to complete dependencies of other ...
18
votes
2answers
12k views

When to use Test.startTest?

I am new to salesforce development and I am quite not sure on when to use the below two methods: Test.startTest() and Test.stopTest() 1) Do i need to surround all my insert, update,SOQL and DML ...
18
votes
4answers
1k views

@future runs in test without Test.stopTest() - Can that be?

For the last years I expected all async Apex means not to run in tests when there is no Test.stopTest() in my test code. But now I have a test without this call and a future method is called. I ...
18
votes
4answers
13k views

Unit Test: You have uncommitted work pending. Please commit or rollback before calling out

So I have a method that takes as a parameter an Id of a object and uses it to make an external callout. Unfortunately I can't seem to figure out any way to test it. @isTest static void ...
17
votes
5answers
20k views

Insert Custom Metadata for Unit Test

I have a controller that is querying custom metadata. I'm attempting to write unit tests to test the queries. Is it possible to insert test custom metadata for the purposes of unit tests?
17
votes
6answers
30k views

How to write a unit-test / test class for trigger?

How do I write a unit-test / test class for a trigger? What are my options? Inline with the trigger? New Class?
17
votes
4answers
6k views

SeeAllData=false and Pricebooks

When writing test cases with SeeAllData=false, you cannot access or insert Pricebooks. Effectively this means that you cannot use SeeAllData=false when working with any test case involving products. ...
17
votes
2answers
5k views

WebServiceMock causing “CalloutException: You have uncommitted work pending” when creating managed package

I have a number of test methods that call Test.setMock(WebServiceMock.class, new SomeWebServiceMockImpl()); where SomeWebServiceMockImpl implements WebServiceMock. These test methods pass when I run ...
17
votes
3answers
3k views

Cool, we can chain batches now! But we can't test them. Do we?

I just used the new feature that allows me to call a SecondBatchable class from the finish() method of the FirstBatchable class. global with sharing class FirstBatchable implements Database....
17
votes
2answers
4k views

How does one use the Aura Test class to test Lightning Components?

By scrolling through the Lightning Reference, I've found that there's an Aura Javascript API class called Test. You can find the API docs at https://na24.lightning.force.com/auradocs/reference.app#...
17
votes
3answers
4k views

How to test that Apex Batch finish() sends an email?

I have a Batch class that sends an Email from its finish method and want to test that. a But....I cannot test it. The the above test fails as there was no email invocation at all. BUT: I see it in the ...
17
votes
1answer
2k views

How to best use the “Test Data Builder” pattern in a database context?

I need to setup test data in proprietary cloud platform (Salesforce.com) where I have no access to advanced testing frameworks. Besides that you can read my code example like it would be Java. ...
17
votes
1answer
1k views

deserializing JSON to object: different behavior in unit test vs API?

So let's say I've got a class that looks like this: public class APIParams { public String name; public String category; public Boolean taxable = false; } Then I have a ...
16
votes
5answers
10k views

UNABLE_TO_LOCK_ROW error while running all apex test classes

In spring13, I am getting UNABLE_TO_LOCK_ROW error in my few test classes while running all test classes in new view provided in spring13 release. But in winter12 I don't get any error like this. Do I ...
16
votes
3answers
3k views

How to delete a Test Class WITHOUT using eclipse

I cannot install Eclipse, even if I wanted to. It is not approved software for my org. How can test classes be deleted from production, using deployments? There no 'active' checkbox when I am ...
16
votes
1answer
8k 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 ...
16
votes
2answers
944 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 ...
16
votes
2answers
42k views

APEX test user creation

This is probably a very silly question but I recall on some forums a conversation in regards to this subject. I'm working on my code coverage for an apex class. For that test I need to use the user ...
16
votes
3answers
2k views

Why is SOSL in Test not working?

I have a Custom Object that contains a long text area with 32.000 chars full of comma-separated Ids. I need to find all such records that contain one or more ids. I tried to implement this in this ...
16
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?...
16
votes
1answer
4k views

How do governing limits work with @testSetup from Spring 15' release

The new @testSetup method is covered pretty well by Jesse Altman in his: New Spring ’15 Feature: @testSetup article but as mentioned in the comments, there is a question about how this new feature ...
16
votes
3answers
4k 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 ...
16
votes
3answers
2k 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 ...
16
votes
1answer
2k views

Creating Holidays and Business Hours in Test Code

I have been creating a Business Hours Calculation engine in the past few weeks and have gotten some great help on a few questions I've already asked (BH in Apex, BH and Holiday Affiliation). I have ...
16
votes
4answers
378 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 ...
15
votes
8answers
9k 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 ...