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
votes
1answer
17 views

What will be the Test Class for this Pie Chart? [closed]

public with sharing class PackageBuilderExtenssion { public PackageBuilderExtenssion(ApexPages.StandardController std) { sc=std; id=std.getId(); //Controller Id ...
0
votes
1answer
21 views

Variable does not exist: Quantity

Variable does not exist: Quantity Although it does exist as I added a value to it and it worked fine this is the code @isTest public class AssetTriggerTest2 { @isTest static void test(){ ...
-1
votes
0answers
16 views

I want to create a test class using this code,.Anyone help me in this one?Thanks [duplicate]

public class AccountSearchController1 { //main class //results variables public Boolean hasResults{get; set;} public List<accountSet> resultSet {get;set;} public List<...
0
votes
1answer
35 views

Test Class for Comparable Wrapper class

I am trying to write a test class for a Comparable Wrapper with little success. The wrapper class pulls in two objects to make one single list. I have two issues... I don't know how to cover the ...
0
votes
2answers
26 views

Inner Class Cover in Test Class

Please help me why my code coverage is showing 0% . See my class code and test class code where I have written wrong? Class public class InnerClass { public class Wrap { public string ...
1
vote
2answers
38 views

Cover Error Handling on Test Class

I am having an issue where I have a trigger that rolls up data from contacts to an account. This was working fine with no issues until we added a managed package app that is constantly checking and ...
0
votes
1answer
26 views

How to insert a dummy value for custom metadata setting

I have a batch class and in the constructor of the class, I have a query in which it queries for a Queue. Constructor: public BatchClass(){ final String QUEUE_NAME = GlobalPropertyCache.getValue('...
0
votes
2answers
44 views

How to assign both (value, label) to picklist field in Unit Test class?

Added explanation: Since the Case.picklistField is constantly modified by other Org user (non-developer), so I want to avoid data dependency as much as I could. I have a case trigger method using ...
0
votes
2answers
58 views

How to login salesforce user programmatically?

I require to login certain users among the test users created in test classes. Each user is created as follows: User user = new User(Alias='test', Email='test@none.com'); insert user; I create ...
9
votes
4answers
189 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 ...
0
votes
1answer
58 views

Unit Test for flows in Salesforce

I have to create a test class for a flow and I wanted to check the flow test coverage for it. https://releasenotes.docs.salesforce.com/en-us/winter19/release-notes/rn_forcecom_flow_test_coverage.htm ...
0
votes
0answers
23 views

Hello i am not able to get the result from this test class [duplicate]

I am trying to update the email in my Test class but it is not happening please help me. This is my class- global class PrepareSandboxPostCopy_Batch implements Database.Batchable { String query; ...
0
votes
2answers
56 views

Test Class - Error - Constructor not defined

Error - Constructor not defined: [PrepareSandboxPostCopy_Batch].() Test Class @isTest private class SandboxPostRefreshTestCase { static Testmethod void testSandboxPostCopyScript() { ...
0
votes
1answer
43 views

Apex Test Class coverage

I test my class and its 50% on trigger handler class, what can I do to cover the lines with the red code; My Class: public class AccountTriggerHandler { public static void updateContactAddresses(...
2
votes
1answer
44 views

Can I execute test method via Anonymous Window?

I am trying to call my Test class's method to see if I can validate the data that is being created as a part of testSetup. Here is my code: @isTest public class Test_Controller { @testSetup ...
0
votes
1answer
25 views

How to test the current logged in user in test class?

I need to cover the below method where I am using the current logged in user in the query. @Auraenabled public static User getUserInformation() { User user1 = new User(); user1 = [...
-3
votes
1answer
85 views

Anyone can optimized this code for better explanation and easy to understand? [closed]

The function of this apex code is when Account has no Contacts the first contact created become the primary contact and when there is existing primary contact it cannot assign a other primary contact ...
2
votes
1answer
39 views

Mock REST and SOAP callouts in the same method

I'm trying to figure out how to test a controller method that does both REST and SOAP callouts in the same transaction. Here is the pseudo code: HelperClass public static void restCallout(){ // ...
5
votes
1answer
45 views

Weird Behaviour of Bulk ContentDocumentLinkTrigger in Test Context

I know that ContentDocumentLink triggers are not bulkified by Salesforce. I was expecting that behavior and was writing test classes accordingly. It looks like, ContentDocumentLinkTrigger doesn't ...
1
vote
2answers
110 views

How to create unit test for Apex Controller that responds to user input?

I have a process that has been built that presents a visualforce page to the user, they first select a price book, and a section on page is refreshed. They are presented with a list of products from ...
0
votes
2answers
57 views

getting error while running a test class,error,Insert failed. REQUIRED_FIELD_MISSING

My controller class is public class Iapc { public boolean detailpage{set;get;} public boolean show{set;get;} public boolean hide{set;get;} public boolean editpage{set;get;} ...
5
votes
1answer
58 views

When should I use SeeAllData=true in my test methods?

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

code coverage not cover fully cover block in unit test

My production code goes like this below: try{ insert githhubUsersToInsert; } catch (DmlException e){ throw new GithubUsersException('create record failed.'); } The unit test report tells that ...
9
votes
1answer
588 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
1answer
140 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 ...
5
votes
1answer
121 views

How do I test asynchronous Apex?

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 ...
8
votes
1answer
289 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 ...
0
votes
0answers
28 views

How to cover set<Employee__c> parameter in static method

how to cover Set of value parameter in test class. please check below my code public class test { public static Decimal testamount(set<Employee__c>Emp, string state){ ---- ----- ...
1
vote
1answer
238 views

JSON.deserialize return null values

I'm trying to Test my Rest Web Service class. I want to simulate the process by taking a JSON and deserialize it to a class - Instead to create all the data by my self. But for some reason - all my ...
-2
votes
1answer
40 views

How to write test class for contact detail page [closed]

My controller: public class contactdetailpage { public contact con{set;get;} public list<ContactActivities__c> ca1{get;set;} public string conid{set;get;} public ...
0
votes
1answer
64 views

Getting to 100 percent code coverage on my trailhead module “Write Negative Tests Unit”

I seem to be stuck on this trailhead module or write negative tests unit, while I have 93 percent code coverage, I can't seem to get the code coverage to hit 100 percent at the "returnValue" piece ...
-4
votes
2answers
51 views

When I am deploying salesforce apex class it is showing error [closed]

I am trying to deploy the apex classes in my target org through force IDE but it showing error, Can you please suggest me where I am doing wrong Deploying showing error log : StaticBusinessLogic....
1
vote
2answers
21 views

Test that production values are still set in sandbox from batch

I have a program object. Each record created from the program object has an external id field that is used for an api integration. If a case's program matches the name of the program record, the ...
1
vote
1answer
22 views

Unit testing addError without doing DML

We're big fans of doing unit tests using as little DML as possible, mocking SObjects using various techniques Here's a simple example public class FoosService { public void doBar(Set<Id> ids)...
0
votes
1answer
18 views

Test class for Page Reference Fails even though values are the same

I am writing a test class for a visualforce page extension. The method upserts a record and then redirects to a custom vf page to add Opportunity products. I get the error System.AssertException: ...
1
vote
1answer
28 views

Tests tab Run All total column value

I did a 'Run All' for my tests from Sandbox Developer Console. Under the tests tab, there is a column named Total. For my all tests it is shown as 8. In my org I have around 200 test methods which ...
3
votes
1answer
32 views

Will system.runas() be running in a separate execution context?

I am using a util class with a singleton pattern to retrieve the custom settings. Like below: private static Additional_System_Settings__c addSysSetting; public static Additional_System_Settings__c ...
9
votes
4answers
150 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 ...
6
votes
1answer
216 views

Automated testing of Lightning Web Components?

Personally, I struggled to get value out of the Lightning Testing Service and felt it was very limited compared to e.g. Angular Testing. Working for an ISV, automated testing is key to keeping ...
0
votes
1answer
28 views

Test class for Batch

hi guyz im new in salesforce can anyone help meh with my code for testclass heres the code: @isTest public class Testclass { @isTest static void Testclass() { List<sobject> lmsin ...
0
votes
1answer
31 views

How to test email notification to the Administrator?

I can't insert right profile to test user like an administrator and don't know how to test it right. Help me please. public void notifyAdministrator(String errorException){ User user = [SELECT ...
0
votes
1answer
30 views

How to test Exceptions (Query and DML) for ContentDocument?

I try to write wrong query, but I get just error un my test, not coverage. If I use try-catch in unit-test - no error and no coverage either. How to test it right? And how can I test here delete DML ...
0
votes
1answer
41 views

test class with Batch and Callout [duplicate]

I have a problem with FeedItems in my Test class, it's very weird behaviour Does someone explain to me why this happens? Callout Class public with sharing class Callout { public static void ...
0
votes
1answer
34 views

Test class for Opportunity whose Stagename is changed to close Won/ close lost, its close date should be today, is this correct?

@isTest public class populateOpportunityTest { @isTest public static void testopportunity() { List<Opportunity> oppList= new List<Opportunity>(); for(Integer i=...
0
votes
1answer
26 views

How to catch update exception and what kind of notification to do here?

I have a trigger with helper class and test. But I don't know how to test update there and what kind of notification I need. Thanks for any help. Trigger: trigger ArchivedTrigger on Item__c (after ...
1
vote
1answer
34 views

Can't access related record in test

I have a visualforce page which is displaying a list of opportunities. The page is working as expected - no problems. The problem is that when testing I can't access the account which is ...
0
votes
2answers
30 views

Code Coverage low even though I'm calling the method

I am coding an Apex Test Class for an Apex Class. I am have trouble understanding why some lines of code are being covered while others are not. I also don't understand why the if statement in my ...
2
votes
3answers
34 views

How do I pass record IDs from testSetup to isTest methods?

I want to create data in a test class that can be used by multiple test methods. However, for the test method to be able to access that test data, I need a way to pass the record IDs to from the @...
6
votes
2answers
194 views

Is this test for callout wrong? How to make a bulk?

I wrote mock and test for callout, but I don't think it's right. I think I check wrong things with asserts and maybe call the same methods a few times, while have another ways to get 100%. Have I? I ...
0
votes
1answer
29 views

Malformed JSON: Expected '[': wrong test, callout or mock?

I have System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set and I don't know how to fix it. Callout: public class Callout { public List<Tool__c> toolsList; public ...