Stack Exchange Network

Stack Exchange network consists of 174 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

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.

17
votes
6answers
29k 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?
8
votes
1answer
286 views

How do I write an Apex unit test?

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 ...
123
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 ...
38
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 ...
25
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 ...
33
votes
6answers
7k 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 ...
7
votes
1answer
138 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 ...
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 ...
28
votes
6answers
22k 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. ...
19
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 ...
15
votes
2answers
23k views

Method defined as testMethod do not support web service callouts, test skipped

I have been struggling with this error for a few days now. I get this error every time I try to run a test for a method that has an httpCallout. I followed the architecture from http://www.salesforce....
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 ...
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 ...
6
votes
2answers
1k views

Getting CalloutException when testing Batchable class with Callouts and HttpCalloutMock

I'm trying to write test code for a Batchable class that AllowsCallouts. I've written a lot of other test code that uses HttpCalloutMock classes, and planned to do so for this class as well. And, ...
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 ...
12
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
4answers
9k 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 ...
10
votes
1answer
3k views

Set Business Hours in Apex Test Code

I have written a class to calculate business hours for Case age in business hours. The class works great my issue is when trying to write decent test code for the class. In following testing best ...
7
votes
1answer
6k views

Test class Rest Webservice parameters

I´m having problems with url parameters (req.requestURI) in my test class. This is my code. REST CLASS: @RestResource(urlMapping='/Accounts/*') global class StoreController { @HttpGet ...
6
votes
1answer
10k views

Test Code coverage catch block

I would know how to cover, with the test class, the catch block in my apex trigger: APEX TRIGGER: try{ update olis_updated; } catch (DMLException e){ for (...
2
votes
1answer
3k views

test fails because of TestMethod do not support getContent call

My below test fails because of Run Failures: TestRangehoodObjectionSheet.RangehoodObjectionSheetTest Methods defined as TestMethod do not support getContent call. I added the Blob thinking it might ...
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 ...
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 ...
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 ...
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 ...
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 ...
13
votes
2answers
5k 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 ...
19
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 ...
12
votes
2answers
3k 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 ...
4
votes
1answer
462 views

Salesforce Test Coverage “Stuck” at 0%

I've got a class and test class that I know are at least 75% covered, but for some reason when running from the Developer console, although all tests pass, code coverage remains at 0% and doesn't show ...
8
votes
2answers
2k views

How to run tests in the default namespace in a sandbox using Ant

I am planning to run SFDC tests for Default namespace in sandbox my build file is as follows - <target name="RunTestDefaultNamespaceSandbox"> <sf:compileAndTest username="${sf.username}" ...
4
votes
1answer
6k views

Writing test code for wrapper class

I created a controller which looks like this public with sharing class EventRegistrationController { // A CONTROLLER FUNCTION // FUNCTION RETURNS A LIST OF cSession public List<...
2
votes
2answers
2k views

“Methods defined as TestMethod do not support Web service callouts” error when testing apex callout

I am trying to write a test for a trigger that is firing a future apex callout : The trigger calls the callBillingService method from this class: public class BillingCalloutService { //Implement ...
0
votes
2answers
3k 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 ...
29
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 ...
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....
18
votes
2answers
11k 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 ...
15
votes
3answers
5k 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 ...
9
votes
2answers
3k 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 ...
9
votes
1answer
584 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 ...
7
votes
3answers
2k 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
10
votes
1answer
2k views

Unit-Testing Sites Permission Involving a Sharing Rule with Unit-Test Created Data

We currently have a Salesforce Site, and we're trying to add some unit-tests, around permissions and sharing rules that need to be set. We've smoke tested everything via the UI, and permissions appear ...
5
votes
1answer
3k views

Querying Time-based Workflow Queue

Is it possible to query the content of the Time-based Workflow Queue from an Apex unit test to assess that a specific Workflow Rule has been executed and added a new item to the queue? I didn't see ...
11
votes
5answers
3k 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 ...
3
votes
2answers
1k views

Case assignment rule is always executed in the test class

No matter what to do, the case is always assigned by the assignment rule!! You can try the scenario and code which are attached in the post. But, I have tried to execute the code in the developer ...
3
votes
2answers
16k views

How to Write Test Class for Standard Controller Along With Extensions

Hi Everyone I'm finding it difficult to write test class for this class Class public class SendEmailReceipt{ public Payment_Receipts__c Rec; public string em{get;set;} public id ...
2
votes
3answers
2k views

Site.createPortalUser() returning null in test context

I have a siteRegistrationController which is creating portal users as below: Registration Controller: public class SiteRegistrationController{ public void createPortalUser(){ //There is ...
8
votes
1answer
564 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 ...
4
votes
1answer
811 views

Package test coverage way lower than actual

So pretty much what topic says. While trying to upload a package I get the following error: However, when I check test coverage anywhere else, it shows me 77%. Developer console: Setup --> Develop -...
3
votes
1answer
8k views

Constructor not defined Error for test case

I have an error that seems common on the web, Though i cant find a definite answer that applies to my case: I have the following Apex code: public with sharing class templicenseemail { private ...