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.

1
vote
1answer
98 views

deployment using change set- run specified test

Hi i have a changeset which has 2 apex class and their test classes with 65% and 80% individually test coverage, if i choose a option of run specified test, will that be deployed? Because one of apex ...
-1
votes
1answer
71 views

How to fix Attempt to de-reference a null object in test for Post Callout?

I become an error System.NullPointerException: Attempt to de-reference a null object in my test: public class Token{ public String accessToken{get;set;} } public static String accessTokenBody(){ ...
1
vote
1answer
28 views

How can I test Post Callout with web token's access? HttpCalloutMock

My POST Method (data for web token - is my custom object's fields): public class Token{ public String accessToken{get;set;} } public static String accessTokenBody(){ //our web token (data is in ...
1
vote
1answer
61 views

How to set the variables of invocable method in unit test

I have an invocable method with varables. I am stuck how to set these in unittest Class public class or_service { public class invoceVars{ @InvocableVariable public id ...
-1
votes
1answer
55 views

How to write test class with invocable with multiple variable [closed]

I am looking at the solution on how to call the invocable method with different data points How invocable method and bulk behavior in Process Builder work? I am calling this invocable method from ...
0
votes
1answer
44 views

How to test access token for HTTP callouts?

I have an access token for my HTTP callouts. How can I test it if I wanna use Static Resource? But I can also use the HttpCalloutMock Interface. it doesn't matter My access token is in custom object. ...
1
vote
1answer
129 views

Method does not exist or incorrect signature: void parse(String) from the type or_propertyJSONTest

I am new to apex callouts and trying to do some JSON parsing. I used the online tool https://www.adminbooster.com/tool/json2apex which worked great. I got a class and a testclass from the ...
1
vote
1answer
40 views

How can I test Callouts with HttpCalloutMock?

How can I test this method? I try the test code from trailhead but can't make any coverage. Callout: public class Token{ public String accessToken{get;set;} } public static HttpResponse ...
2
votes
1answer
93 views

Receiving error “Org cache is only supported in Data Silo tests”

I've implemented platform cache, I'm running a test and i received the following error: caused by: cache.Org.OrgCacheException: Failed Cache.Org.contains() for key 'timeZones': Org cache is only ...
0
votes
1answer
39 views

'Last Viewed By' Account field test - System.QueryException: unexpected token: 'null'

Trying to create a Last Viewed By field on Account. I'm newbish at Apex and not seeing what I'm missing here... I'm sure it's a rookie mistake. Adapting code from http://www.sfdcpanther.com/setting-up-...
0
votes
1answer
26 views

Correct HttpPatch and correct test for it

I need to update a right record with ExternalID = Id of inserted record. And I try to check this field (ExternalID) in SOQL, but get an error System.QueryException: List has no rows for assignment to ...
0
votes
1answer
45 views

How to avoid: Error uncommitted work pending in my testclass

I am doing do a callout trough a processbuilder with a future call. Before I do the call I get some variables from a custom setting. When I do an insert in my testclass for my custom setting I get the ...
0
votes
1answer
36 views

Test Class Not Covering Original Class

So I am trying to cover this Chatter Answers Class so I can increase my system's code coverage. The test class runs successfully but the original class is still 0% covered. What do I need to do to ...
1
vote
1answer
126 views

Test for HttpPost service: how to take JSON body for test?

I have a HttpPost methods. And I really don't know how to test it correctly. My methods: public static App__c ParseRequest(RestRequest req) { App__c app = new App__c(); String body = req....
0
votes
1answer
33 views

How to make Test Coverage 100%? HttpGet method

How can I test this for 100% coverage? @HttpGet global static List<Box__c> getBox(){ RestRequest req = RestContext.request; String boxId = req.params.get('Id'); if(boxId == null) ...
0
votes
2answers
38 views

Test trigger before insert

I'm trying to test a test a trigger that adds an error if the field Active__c is set to true on CreditInfo_Eval_Rules__c (son of CreditInfo_Fields__c) and the field Used__c is updated to false. I'm ...
1
vote
1answer
31 views

What kind of test can I write to check the file?

I have a controller for VF Page. This code upload a profile's image (we can see it after that) and delete it. What kind of test can help me to check this file? I need to know, is it uploaded or not. ...
0
votes
1answer
91 views

Static method cannot be referenced from a non static context (PageReference)

What is wrong here? Candidate__c cand = new Candidate__c(); insert cand; PageReference pageRef = Page.UploadCandidatePhotoVF; Test.startTest(); Test.setCurrentPage(pageRef); ...
0
votes
2answers
41 views

Make the test work: File insert, check this File

This code is a controller for my VF Page. It inserts ContentVersion and ContentDocumentLink. After that a candidate field Photo__c take a ContentVersion.Id value. How to check it correctly? Now ...
0
votes
1answer
14 views

Why does my country returns null on creating an account?

I am creating a testdatafactory. creating a country goes well. When creating accounts my country fields are null if i debug CountryList it returns the created country record. Factory class @isTest ...
0
votes
2answers
41 views

How to build a testdatafactory without error : DUPLICATE_VALUE, duplicate value found: duplicates value on record with id: : []

I an trying to create a testdatafactory which i can use to call in my tesclasses. What I want to achieve is small buildingblocks with which I call another method if a lookupfield needs to be filled. I ...
0
votes
2answers
19 views

do I need to update or insert a list, set or map in testclass

When ik create a list, set or map and add records to these do I need to update / insert after adding records. I did a test system debug on a testclass with only a return. My debug returns a list with ...
2
votes
1answer
60 views

Is there any way to set set salesforce userid from apex test class

Is there any way to set set salesforce userid from apex test class. For Example: SetupTest st = new SetupTest(); Account billing = st.Account('Billing'); billing.Name = '...
2
votes
2answers
45 views

Code coverage annotation for or statement next line or same line?

Ik have a class with several or statements. To make the code more readable I placed the or statements on a next line. My dev console shows that there is no coverage for these lines. Do I need to place ...
0
votes
1answer
180 views

Script-thrown exception in Test class for Batch apex callout

I have a batch class which makes multiple http callouts to an HR system and then with the absences returned checks for existing absences and upserts. However when I try and run test class for the ...
-2
votes
2answers
60 views

Methods defined as TestMethod do not support Web service callouts -ApexPages.StandardController controller [duplicate]

I am trying to test my Apex class, getting following error response "Methods defined as TestMethod do not support Web service callouts" public with sharing class CreateOrder { private Boolean failed ...
-3
votes
1answer
34 views

Unable to cover apex Test class

Apex Class public class get_data{ public static map<string,map<String,String>> get_tier(String fetchFrom){ map<string,map<String,String>> dataFields = new map<...
1
vote
0answers
23 views

Unaltered Test passes in Sandbox AND Production, but fails during unrelated deployment validation

I am trying to deploy a few classes. I've got a Sandbox that I literally created this morning off of Production. Nothing is different between the two except the 3 classes I'm deploying. How can it ...
0
votes
1answer
37 views

INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY when System.runAs(u) in a test class

I need to create a Patient_Registration__c (which is a Person Account) under a specific user in my test class. Following is my code in the test class: Profile p = [SELECT Id, Name FROM Profile WHERE ...
4
votes
2answers
118 views

Apex Question regarding Declarative Rollups for Lookups Solution

I am trying to utilize DLRS with objects that are used in the Field Service Lightning managed package. More specifically, I am trying to total the ActualDuration field from the ServiceAppointment on ...
5
votes
3answers
104 views

Use System.Assert on Declarative Features

I hope this is not a foolish question but would there be any value to writing test classes for Flows and Process Builder? Not to check code coverage but to use assertions. I am starting to get better ...
0
votes
1answer
133 views

System Assert Exception in Test class of Case Trigger

We have a very old Case trigger which has a condition for a particular record type if the Task is open then case close should not close the case, Now the business user want to lift that restriction, ...
4
votes
1answer
90 views

Why won't Apex IF statement work in Production Unit Testing after working in sandbox?

this is driving me NUTS and any thoughts are MUCH appreciated after looking at this for far too long. Use case: users in certain profiles need to be automatically added/removed from a particular ...
8
votes
2answers
391 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 ...
0
votes
1answer
61 views

System.ListException: List index out of bounds: 0 when list is not empty

I have an apex code like that : List<Campaign> campaigns = DataFactory.createCampaign(); Contact contact = new Contact(/*some fields here*/); if(!campaigns.isEmpty()){ ...
1
vote
2answers
31 views

Test & datas - An issue with IDs

I'm working with class tests, I'm trying to use best practises so I got : - one class I want to test - one class for testing I got a problem when inserting my datas : @testSetup creates Accounts, and ...
2
votes
1answer
32 views

Formula field not evaluated in trigger.new in test class only

I am writing a test class for a trigger (after update in this case), and while the code works on my person account record page when I inline edit, it does not work in my test class when I manually ...
1
vote
1answer
27 views

How to test a change OwnerId by groupQueue.Id?

I have a trigger. If a user who creates a record isn't 'Manager' (UserRole.Name) - record's OwnerId change to GroupQueue.Id. I have a correct test method for scenario, where user-creator is 'Manager', ...
0
votes
1answer
196 views

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY - INVALID_CROSS_REFERENCE_KEY

I'm also getting a the following error from my apex test and trigger while validating. The error,test class and trigger are below. Run Failures: TestAfterPOLogUpdateTest.myUnitTest System....
-3
votes
1answer
44 views

Test Class for triggers 1 [closed]

I have written a trigger and trying to write a test class for it.I am not getting the code coverage. Can u pls help me where i am going wrong Trigger trigger ProfileSkillUser_AfterInsert on ...
0
votes
2answers
89 views

It can't see my Queue: “Expected: 0, Actual: 1”

System.AssertException: Assertion Failed: Expected: 0, Actual: 1 How can I fix it? Group testGroup = new Group(Name = 'XXX', Type = 'Queue'); insert testGroup; List<Obj__c> oList = [...
0
votes
1answer
38 views

How to mock a private property in controller correctly?

I have a controller with a private property that holds an instance of an object. The code below is reduced from unnecessary details. public with sharing class BarController { private Foo ...
0
votes
1answer
30 views

Create a community in test without use seeAllData=true

do you know if it is possible to create a community in test class without to use the feature seeAllData=true I would like to create a community, create community users and assign users to the ...
0
votes
1answer
139 views

Too many async jobs enqueued for this apex test context

I have faced following issue while running the test class.I am trying to insert 1000 records in test class ,the object have scheduled process builder.i am facing following error recently. Error: ...
0
votes
1answer
30 views

Test Class for testing controller twice

I have a test class below that is successful and covers accessing my controller with a callout: @isTest static void testUpdateOfPack(){ MockPackageService mockRes = new MockPackageService(); ...
0
votes
2answers
91 views

GET API callout in test classes

I have a web service callout that checks if an order’s type is return and needs to see what the actual boughtPrice is: webservice static String updateBoughtPrice(Id ordId){ Decimal boughtPrice = ...
0
votes
1answer
28 views

How can I test Messaging.MassEmailMessage with checking targetobjectids?

We have some User with UserRole. I need to verify that the email has been sent to the user with the role 'TestRole'. How can I do that? I try: List<Messaging.MassEmailMessage.targetobjectids> ...
1
vote
2answers
620 views

Code coverage error when deploying to another sandbox, but Salesforce doesn't show which classes are missing coverage

As the title says, I'm trying to deploy some code and metadata from a sandbox to another. The deployment compiles successfully, and the tests pass too, but the deployment fails with the "Code coverage ...
0
votes
1answer
37 views

How to use different System.assertEquals() in two separate methods?

I have the following test methods. Test method #1: public static List<Account> testAccountsMethod1() { List<Account> accounts = new List<Account>(); for (Account ...
-3
votes
1answer
34 views

Test class with different scenarios for trigger helper

I have 100% coverage, but I still haven't scenarios for check. How to write good and bad scenarios for test class? This is my helper: public with sharing class JobHelper { public static List<...