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
0
votes
2answers
2k views

Apex test class method issue

Here I have @Auraenabled apex class I'm trying to write a test class but it's throwing an error like below Static method cannot be referenced from a non-static context: List<AuraSampleController....
-2
votes
1answer
38 views

Test Class for a simple Apex Class [duplicate]

I am trying to write a test class for a simple Apex Class which sets the status of the Case to be Closed like below public class updateCaseRecord { @AuraEnabled public static void ...
0
votes
0answers
23 views

Mixed DML causing test classes to fail to to Process builder

I have a Test class which was working fine but after a new process builder was created on user object to update a custom field created on user object, it started failing. In my test class code I am ...
-1
votes
2answers
25 views

How do I test the FOR loop data for this code?

I have written a test class for a class and its coverage is above 75% but I want to increase it and found that a particular for loop is not being covered(written Between ** below). Please help me with ...
0
votes
1answer
20 views

Batch callout error: Call out made it firstbefore DML

I have review similar questions, and all of them mention to do API before any DML similar question 1 and similar question 2 However, I am trying to do the API in the excecute, and I am still getting ...
1
vote
2answers
81 views

Apex Unit Test for if == null and return statements

I have a simple helper method in my Apex Code that loads an external ID from a field on the related Case.Contact. I have problems to write a proper unit test that covers the last three lines, ...
-1
votes
1answer
44 views

Issues with Apex Test Class

Can you help us with a test class? We are using the class in a LWC and we confirmed that it works when we test it manually. The coverage test stops at around at line 9 after the json serialize lines. ...
0
votes
1answer
29 views

Why should we use system.assert in test class even after enough code coverage?

How to use system.assert in the below test class? @isTest public class a_Call_Objective_Trigger_Handler_Test{ testMethod static void CallObjectiveTest(){ RecordType Callrt = [SELECT ID,...
7
votes
1answer
973 views

Testing Platform Cache usage

When writing Apex test cases for code that utilizes the platform cache what are the important scenarios I need to cover? Additionally, how would I address those is the test methods? For instance, ...
0
votes
1answer
25 views

Test class passing but not getting any code coverage

so I am newer(ish) to Apex still and clearly know that I am not getting any code coverage to my Apex class because I am not referencing the method, my question is how exactly would I go about calling/...
1
vote
1answer
200 views

Code Coverage - Salesforce Connect Custom Adapter / Datasource.Connection Class

I am unable to find documentation on or get any code coverage for the customer adapter Apex Class (Datasource.Connection Class). For awareness, the external data source and customer adapter functions ...
0
votes
2answers
558 views

Workflows are not running for Unit tests

I recently came across an Issue when running a Test class. In my Test class I Insert a custom object. And by the time the record is inserted, there is a workflow rule to fire and populate the Id value ...
26
votes
2answers
6k 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 ...
0
votes
1answer
31 views

What is wrong with this test class? It is not covering, any suggestions? Thanks, [duplicate]

@isTest public class PropRentTotalSumTEST { @isTest static void testFeeCalculation() { Property__c prop = new Property__c(); prop.Name = 'Test'; prop.Monthly_Rate__c = ...
2
votes
1answer
35 views

Running the packaged tests while installing with sfdx:force:package:install

Is there a way to run packaged or org tests when we install a Managed/Unlocked SFDX package to an org using sfdx force:package:install?
2
votes
2answers
1k views

Apex test is slow during deployment, but fast any other time

I have a test that takes about 5 minutes to run during production deployment. If you just click "run test" or run it from execute anonymous, it's not slow at all (a few seconds). Is there a way to ...
0
votes
0answers
24 views

How to test method that uses Site.validatePassword(…)

I'm writing tests for a custom Apex class regarding the self-registration of a customer community user. One of the methods contains the API call to Site.validatePassword(user, password, ...
0
votes
1answer
21 views

How to test a scheduler class scheduling itself and calling a batch. Check out the code

I have a batch class which is called by an scheduler class which is scheduling itself. I have written a test class which has two methods: One tests the batch and the other the scheduler.Overall the ...
0
votes
1answer
13 views

Test for Generated Class From WSDL

Should I write unit test for those apex classes generated from wsdl when deploying to production?
0
votes
2answers
47 views

Why does controller gives: System.NullPointerException: Attempt to de-reference a null object

I have an apexclass with a pagereference for which I am writing a unittest but this returns an NullPointerException on controller.toCareerPageWithFilters(); what am i missing? public class ...
0
votes
0answers
12 views

Does link expiration change when sending previews/test? [closed]

Thought id ask - I have a bunch of view as webpage links ive given that were taken from test emails using the preview and test - just wanted to make sure these links don't expire earlier compared to ...
0
votes
0answers
24 views

Test class for the code below [duplicate]

// Hi Community, I need a help to write a test class for this code, can someone help me with? public with sharing class PropRentTotalSum { public static void RentTotal(List<Fee__c> ...
4
votes
2answers
83 views

Does MIXED_DML_OPERATION behave differently in test methods depending on how you run them: Test Execution vs. Deployments?

We typically validate our source control nightly and run all tests. This is essentially a validate only deployment running all tests against production. All tests pass. However, if we were to run ...
0
votes
1answer
22 views

How to test picklist controller apex class

I have a lightning component that works just fine but I need to test a controller: public class PickListController { @AuraEnabled public static List<String> ...
0
votes
0answers
18 views

How to mock multiple post callouts in test - different responses same endpoint [duplicate]

I have some code that is potentially making multiple post requests to the same endpoint within one transaction, then updating records based on the response. I think the code works, but I want to ...
0
votes
1answer
372 views

Send email failed, No body supplied for file attachment

I am writing a test class with testMethod function. In the function i am trying to send mail from a test class but getting an error. The error is System.EmailException: SendEmail failed. First ...
-1
votes
1answer
38 views

How to write test for this trigger [duplicate]

Want to write Test case and unit test for this trigger trigger Autoleadconversion on Lead (after insert, after update) { for (Lead lead : Trigger.new) { if (lead.isConverted ==...
0
votes
1answer
27 views

Test Class for Apex Batch Job generating Invoices covers only 38%

Please help me to finish my Test Class for the following Batch Job generating Invoice creation. Apex Batch Job global class InvoiceGenerateBatchJob implements Database.Batchable<SObject> { ...
1
vote
0answers
13 views

How Test LogoutEventStream

I am creating a functionality to count the number of minutes that a user is connected. For this I use the LogoutEventStream in a trigger. The problem is that I don't know how to test it. I already ...
1
vote
0answers
7 views

Dynamic Test Class for Trigger in managed package

Problem Using a managed package and Metadata API, we are deploying a trigger class that calls to a global packaged class. We are deploying also the Test Class dynamically for the object where we are ...
3
votes
1answer
216 views

Unable to Insert ContentVersion record as a Community User in Test Class

I'm unable to create ContentVersion records running a test class as a Partner Community user. I have created a Partner user using the same methods displaying in the test class, tested the process ...
0
votes
1answer
31 views

Unittest eror: System.NullPointerException: Attempt to de-reference a null object

I am writing a unittest for my apex class but get an error System.NullPointerException: Attempt to de-reference a null object on my unittest for variable: testoptions Apex class public class ...
2
votes
1answer
9k views

How to bypass Validation rules when running apex class?

We have a managed package install in our org. When I create an opportunity, at a certain stage, I need to create a record of that managed package. For this we define validation rule for certain stages....
0
votes
1answer
18 views

How to test a List<SelectOtion> in unittest

How to test a List<SelectOtion> in unittest. I have the following apexclass public class WebToObjAttachments { .. public List<SelectOption> CountryOptions { get { ...
0
votes
1answer
670 views

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION

I wrote the validation rule to restrict the custom field picklist value it many test classes is getting failed because of that where and what line i can add into picklist value to cover the test ...
24
votes
5answers
34k 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?
2
votes
2answers
529 views

Assign JSON Values to Fields in Test

Markup <apex:page controller="skillproficiencycontroller" id="p"> <apex:form id="f"> <script> var state = {}; function passState() { document.getElementById("...
0
votes
1answer
20 views

How to avoid in unittest Error: Invalid type: ZuoraObject.ConvertToZuoraInvoiceDatas

I have a following class with method public virtual class ZuoraObject { .. public static List<InvoiceData> ConvertToZuoraInvoiceDatas(List<Zuora.zObject> zInvoiceDatas) { if (...
0
votes
1answer
30 views

Help to get 100% test coverage for apex class requiring to populate only one of the two custom fields

Here is my apex class with validation rule to populate one of the two custom fields, and not allowing to populate both custom fields at the same time: public with sharing class FieldnotEmptycontrl { ...
-1
votes
0answers
25 views

Test class - INVALID_CROSS_REFERENCE_KEY, Record Type ID: this ID value isn't valid for the user [duplicate]

I have a test class that does not use 'run as'. I am getting this error when I try to insert a custom object record - INVALID_CROSS_REFERENCE_KEY, Record Type ID: this ID value isn't valid for the ...
10
votes
2answers
753 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 ...
1
vote
1answer
773 views

Fatal Error Deploying Apex from Sandbox to Prod

I made some small tweaks to an apex class in sandbox and all my unit tests passed in sandbox, I tried pushing the apex classes and the unit tests. The error I'm getting is Fatal Error and that my ...
-1
votes
0answers
27 views

Unable to pass Test Class Class Error System.QueryException: List has no rows for assignment to SObject [duplicate]

i am unable to pass a test class because of the above error can anyone pls tell me the solution,here is the code of test class and class @isTest static void validate_AccountCallCleanUpBatch_MRG() ...
7
votes
1answer
7k 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 ...
1
vote
0answers
22 views

How to get code coverage with logic based on screen flow variables?

I am trying to get code coverage to deploy a class that depends on variables set in a screen flow. I've read various articles saying it's not possible to get coverage for screen flows. Is this ...
-1
votes
0answers
11 views

Need help with Salesforce Test Class [duplicate]

This is my Apex controller public class QuoteController { //Get Opportunity_Quotes__c Records @AuraEnabled public static List<Opportunity_Quotes__c> getQuote(Id id,String ...
0
votes
0answers
36 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 ...
1
vote
1answer
221 views

Field not writable: ContentVersion.FileType

In the following test method: static testMethod void testEmailWithAttachments() { ContentVersion contentVersion = new ContentVersion(); contentVersion.FileType = 'Text'; ...
1
vote
1answer
39 views

Best tool for seeing highlighted covered Apex Code while writing test class

Currently I do most of my development in VScode using sfdx. For writing test classes, I need to see which lines are covered in Apex classes I write. In DX, running commands (see below) to get ...
0
votes
1answer
372 views

Response not received using MultiStaticResourceCalloutMock Class

I have below piece of code in an apex class that I am trying test. HttpResponse res = http.send(req); String responsejsonString = (String)JSON.deserialize(res.getBody(), String.class); List<...

1
2 3 4 5
77