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
13 votes
2 answers
1k 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 ...
Keith C's user avatar
  • 136k
13 votes
2 answers
426 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 ...
Uwe Heim's user avatar
  • 28.3k
13 votes
4 answers
21k 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 ...
Chris K's user avatar
  • 293
13 votes
2 answers
1k 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();...
Matt Lacey's user avatar
  • 25.6k
13 votes
0 answers
4k views

Test cases failing with "Could not run tests on class 01pXXX because: connection was cancelled here"

When running all the test classes in my dev org I'm getting intermittent failures on some of them with: Could not run tests on class 01p400000000001 because: connection was cancelled here The ...
Daniel Ballinger's user avatar
12 votes
5 answers
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 ...
Avinash's user avatar
  • 2,101
12 votes
3 answers
27k 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. ...
Bforce's user avatar
  • 6,828
12 votes
1 answer
28k views

No more than one executeBatch can be called from within a test method

This is my test method for batch process: static testMethod void TST_testBatch() { Test.startTest(); testBatch b = new testBatch(); Database.executeBatch(b); Test.stopTest(); } ...
Nagendra Singh's user avatar
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
12 votes
1 answer
16k views

Apex Testing Query : System.FinalException: Testing already started

@isTest public class CaseUtilityTest { @isTest static void TestCreateCase() { Test.startTest(); CaseUtility.CreateCase(100,false); // 1 Test.stopTest(); Test....
Iamauselessrightbrainer's user avatar
12 votes
2 answers
4k views

How would I test a trigger on non-writable SurveyResponse object?

I'm using Salesforce Survey for one of the requirements. I would like to write a trigger on SurveyResponse object. I've written the trigger and apply the logic that I need. Now, I'm trying to write a ...
Anurag Bhardwaj's user avatar
12 votes
1 answer
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....
1337holiday's user avatar
12 votes
1 answer
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 ...
smohyee's user avatar
  • 3,814
12 votes
2 answers
987 views

Comparison fails when converting Opportunity from/to JSON

I am facing an issue when converting an Opportunity to and then from JSON. If I put that in a test, the assertion fails when comparing both Opportunities. Here is my code: Test case: static ...
Juan Alvarez's user avatar
12 votes
1 answer
5k views

RECORD_IN_USE_BY_WORKFLOW, Unable to convert lead that is in use by workflow: []

In my Apex class i am converting leads. When i test my code through UI it is working fine but when i run my test class it get failed and shows this error: 22:21:47.413 (413465000)|FATAL_ERROR|...
AnuRaj's user avatar
  • 2,482
12 votes
0 answers
296 views

Is there a way to force a Platform Event Trigger to honor the PlatformEventSubscriberConfig in unit tests?

I'm attempting to move an integration away from using Custom SObjects and to using Platform Events out of a concern for storage space (and also so our eventual automation of this integration won't ...
Derek F's user avatar
  • 61.3k
12 votes
1 answer
727 views

Creating Idea Votes from portal user in Test code. DML Exception

I Wrote a very simple VF Page and Controller to show the Accounts that voted on certain Ideas. Here is the controller. Just aggregates the Up and Down votes on the Idea excluding internal votes. ...
Chris Duncombe's user avatar
11 votes
3 answers
998 views

Apex Test Suites - Spring 16 release

Salesforce defines Test Suite as a predefined-set of test classes that you can execute at once. Can I use a single @testSetup method to set up the test data for all test methods in one Test Suite? ...
sam_s's user avatar
  • 966
11 votes
6 answers
2k views

How do you unit test a trigger when you don't know the required fields?

This is another issue addressed largely towards app developers. Let's say I write an insert trigger. When I write the test case for it, what I would normally do is create a new object, insert it, and ...
Ryan Elkins's user avatar
  • 7,677
11 votes
1 answer
29k views

Inserting test record with record type

I'm trying to create a unit test where a I insert a record with a specific record type Student_Records__c endterm = new Student_Records__c(); endterm.School__c = schl.id; endterm....
Rufus K.'s user avatar
  • 505
11 votes
1 answer
15k views

When I load the developer console, I get a "Total Requests Limit Exceeded" error

When I load the developer console, I get a "Total Requests Limit Exceeded" error. This is on a dev org. I have been using the Force.com IDE all day, and have been writing a lot of unit tests ...
edgartheunready's user avatar
11 votes
1 answer
4k views

How To Cause DmlException - Delete

I thought I would be able to take a User who does not have delete privileges on an object, delete records from that object, and get a DmlException. No dice. Below are the Case permissions for the ...
Adrian Larson's user avatar
  • 150k
11 votes
4 answers
5k views

Test case failures due to VF email template

First, a little background information: I'm working on an end-to-end custom quoting solution that leverages the standard Quote object. We also utilize Sterling CPQ, which serves as the end-state for ...
JCD's user avatar
  • 3,384
11 votes
3 answers
6k views

Salesforce Trigger - "DML not allowed on RecordType"

I have created a trigger which is dependant on a custom record type existing. In my test for the trigger, I want to ensure that when this record type doesn't exist the trigger throws the appropriate ...
Jim's user avatar
  • 1,126
11 votes
4 answers
1k views

Is MIXED_DML_OPERATION in unit tests broken?

We have a nightly integration that runs all tests and reports any errors against production, currently on Winter '19. Everything was fine until January 14th, 2019, when 13 previously passing tests ...
sfdcfox's user avatar
  • 489k
11 votes
2 answers
3k views

How do you Apex Unit Test Object(CRUD)/Field(FLS) Permissions in an Org Portable manner?

Specifically, how might one get an org portable Apex Unit Test User that DOES NOT have READ access to Accounts? Creating a Test Permission Set that DOES NOT have READ access to ACCOUNTS and assigning ...
mjgallag's user avatar
  • 965
11 votes
2 answers
2k views

"Callout loop not allowed" in unit test for Queueable that implements HttpCalloutMock

I'm attempting to do a unit test for a Queueable that does an Http callout. When I run the test I get an exception: "System.CalloutException: Callout loop not allowed". Previously I was using @...
Phil Lawrence's user avatar
11 votes
1 answer
1k views

Developer Console and Package Visibility Error

Not sure if this is a bug in the Developer Console...? I am building some simple test classes that reference static methods in a managed package. Everything works great when I build and update the ...
BritishBoyinDC's user avatar
11 votes
0 answers
2k views

How do you use a mock response when calling Auth.JWTBearerTokenExchange.getAccessToken()?

Let's say I have a class like this /** * Example of using JWTBearerTokenExchange to demonstrate issues with testing and mocks */ public class JwtBearerTokenExample { public static String ...
jon_wu's user avatar
  • 777
10 votes
1 answer
11k views

How Many Starttest() and StopTest() can I have in One Test Class?

The concept of testing org is still very new to me. I don't think I fully understand it. But can I've more than one StartTest() and StopTest() in my test class as my test requires many cases to ...
user7123's user avatar
  • 863
10 votes
5 answers
15k views

System.CalloutException: You have uncommitted work pending. Please commit or rollback

I am sending only a single HTTP Request in Batch but still facing error System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out My test code is @...
mathlearner's user avatar
  • 2,151
10 votes
3 answers
21k views

1.how to stop run all tests? 2.validation running for about 6 hours in production. how to stop it?

How to stop run all tests? I have clicked run all tests in my sandbox and usually gets completed in about 15 minutes. Now its running for about an hour and still not completed? Note :- Mine is a ...
Sathya's user avatar
  • 3,202
10 votes
4 answers
4k views

Using try, catch and finally in test class (dreamhouseapp)

In the test classes of the DreamHouse Sample app all the functions have the following structure: static testMethod void testSomething() { Boolean success = true; try { ... } catch (...
martes's user avatar
  • 127
10 votes
6 answers
3k views

Scheduling Run All Tests

is there a simple way to schedule the run of all test classes? I want to avoid having to do this manually every day. Thank you!
user9862's user avatar
  • 335
10 votes
6 answers
12k views

developer console: clear tests list

I need to test a bunch of test classes. The test runs are collecting in a huge list so i do not get an overview anymore. Is there any way to clear this list? Debug -> Clear seems not to work with ...
Sergej Utko's user avatar
10 votes
5 answers
6k views

How to perform mock testing on database queries?

In java , we perform mocking to test the database classes. Is there any way to test the classes in apex language & mocking also works in apex test cases. Now how will i perform these test cases ...
Amit Agrawal's user avatar
10 votes
1 answer
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 ...
Mikey's user avatar
  • 2,968
10 votes
1 answer
6k views

Spring 13 Test methods must be in test classes Error

Is someone getting the error "Test methods must be in test classes" I am using @Istest annotation inside the class: public class someclass{ @istest public static void testme(){ ... } ...
Rao's user avatar
  • 16.7k
10 votes
2 answers
5k views

Unable to unit test code that publishes platform events

I keep getting a System.UnexpectedException whenever I run unit tests for code that publishes Platform Events. As soon as I call Test.stopTest(), the test fails with System.UnexpectedException: Error ...
Zach Field's user avatar
10 votes
2 answers
1k views

Unit test SOQL limit setting up data model

I have run in to this situation where the data model is very complex and since SF doesn't support mocks (e.g., Mockito) there are so many SOQL executions in just setting up the data model (triggers, ...
Peter Knolle's user avatar
  • 29.1k
10 votes
3 answers
3k views

What classes and triggers have 0% code coverage?

I know that Salesforce is trying to push the developer console. Hence they removed the ability to see the code coverage in the classes view. The problem is that when I open the developer console and ...
Jose Chama's user avatar
10 votes
1 answer
3k views

Test code coverage reduced to 0%

I have some very unusual things happening with my test code coverage in Production. The coverage for all of my classes has suddenly been reduced to 0%. How does this happen ? Would this happen if ...
Zoom_v's user avatar
  • 3,860
10 votes
1 answer
3k views

Any good reason test method will clear up static variable values set by testSetup method?

Let this sample code demonstrate what I mean here: @isTest public class TestSample { public static String tmp; @isTest static void Test1() { System.debug('tmp in Test1: ' + ...
Lance Shi's user avatar
  • 13k
10 votes
1 answer
2k views

Apex Test Setup

I have a test setup method in my test class: class someTest { static boolean ok; @testSetup static void setupTestData() { ... ok = insertSomeTestData(); } ...
Obi_77's user avatar
  • 475
10 votes
2 answers
8k views

Mock Testing with Named Credentials

I've successfully implemented Named Credentials for a Basic Auth integration callout. However, when I went to write my test method, I noticed one minor annoyance. When I try to create a ...
NSjonas's user avatar
  • 10.1k
10 votes
2 answers
735 views

Does assert methods needed in @testSetup?

I ran force.com security scan on my managed package i got the following report Query: Test Methods With No Assert L 4: @testSetup static void defConnSetup(){ so is it necessary to add assertion ...
Saravanan Baskar's user avatar
10 votes
4 answers
2k views

Generating Real World Type Data

How does one generate real world type data and populate Salesforce test instances with this data that may be needed for testing? Does Salesforce provide any services to generate/provide this type of ...
Giri's user avatar
  • 101
10 votes
1 answer
728 views

Why does an autonumber sequence get "stuck" when creating records after a @testSetup method?

I ran into this while writing unit tests for my org but I simplified it in a developer account to make sure it would happen there. I have custom object Thing__c. The Name field is autonumber. I wrote ...
Pedro Otero's user avatar
10 votes
1 answer
427 views

Independent Auto-Number Sequence

How do I specify API 30.0's new Independent Auto-Number sequence feature for tests run while migrating code from Sandbox into Production? I see the checkbox for this on Test Options, but how do you ...
newbie's user avatar
  • 298
10 votes
1 answer
2k views

Get Status Updates When Running Tests with ANT Migration Tool

I'm trying to set up Jenkins to automate a daily task of running all tests in my sandbox (and eventually, production environment. Typically, when I do a deploy or retrieve task, I get lots of output ...
Benj's user avatar
  • 11.2k

1 2
3
4 5
95