New answers tagged

1

You need to "construct" an instance of the controller, then call its methods. The constructor is the method with no return type and has the name of the class: public pairHouseholdController(ApexPages.StandardController controller) { This method wants an ApexPages.StandardController that has the account, so we need to do that first: ApexPages....


0

Unit tests run in system context mode when executed, should you wish to run a test as a specific user you can use system.runAs(User){ //code to be executed as this user } Its worth pointing out that you will likely want to insert the user rather than creating on the fly as the user id you set here will be null. pageRef.getParameters().put('id', String....


0

The reason for your error is here: update updatedOrder; orderTriggerhandler.afterUpdate(new Map<Id, Order>(updatedOrder),new Map<Id, Order>(updatedOrder)); You are passing the Sobject updatedOrder directly to your trigger handler but the fields that SFDC sets once an Order's status changes to Activated aren't in the sobject updatedOrder. You ...


0

There are a few things that you can improve on that test class. You shouldn't use data from your org, instead you should build your on test data. But I think you can take care of that latter. On the Order record creation I see that you are calling a factory to do so. Can you check if you are inserting these fields on that Order creation execution? Are you ...


3

Two things I see here: If you want to evaluate changes to the opportunity, then you need to requery it after the OCR trigger has fired. ie, after your Test.stopTest(), requery the Oppportunity and then make your asserts. I don't see how your commented out System.assertEquals() statements about the contact lookups will ever be true, even after you requery. ...


1

Your unit test attempts to execute the functionality under consideration by performing DML inserts and updates. However, your post does not contain - and your code coverage suggests you either do not have, or have failed to deploy - an Apex trigger. The classes TaskTrigger and CaseClass will not be called unless an active Apex trigger is present on the ...


0

Quoting a section from this help topic: https://help.salesforce.com/articleView?id=security_sharing_rule_types.htm&type=5 You can’t use Apex to create a criteria-based sharing rule. And you can’t test criteria-based sharing using Apex. I'm assuming the new Guest User Sharing rules fall under this bracket and therefore can't be used in Unit Testing. ...


1

You can't create DML statements before calling your test API callouts. You should put all your DMLs in a separate function with a @TestSetup static Lead lead1; @TestSetup static void makeData(){ Lead lead = new lead( FirstName = 'Test First Name', LastName = 'Sample Last Name', Phone = '555-555-5555', ...


0

Methods defined as TestMethod do not support Web service callouts Stack Trace: null This error occurs because web service callouts are not allowed in Test class. To bypass callouts add HttpcalloutMock class. Here is a sample test class with the mock class to bypass web services Callout. ********************** Test Class *************************** @...


0

There is a Trailhead module on Testing Your Metadata Deployment. To sum up, metadata deploys won't actually execute in unit test context (that would result in unit tests have side effects that violate test context isolation). You can test your callback by calling it directly with synthesized parameters. As for the deployment code, your only real route is ...


1

The answer is yes. Got a super quick answer here: https://github.com/salesforce/sfdx-lwc-jest/issues/128 to use: https://github.com/MailOnline/jest-tap-reporter I couldn't get it to work just configuring in package.json but this did work: "scripts": { "test:unit": "sfdx-lwc-jest -- --reporters='jest-tap-reporter'" }, On reflection I'm going to use ...


0

FYI, I took a look at putting wrappers around the SObject/SObjectType/SObjectField as below but on balance think this would be Test Driven Damage so won't go that route right now. The root problem that got me looking at this is that patch orgs for managed packages that only make dynamic references to other packages don't get the other packages brought over ...


0

When you are inserting the account Account acct = new Account(Name='Test Account'); acct.Last_Subscription_Detail_Update__c = DateTime.now(); insert acct; you have populated the Last_Subscription_Detail_Update__c with Datetime.now(). Now to fire the update trigger you are updating the same field with same value. So salesforce is not detecting any update ...


0

I see several issues with you test code: Your div[data-id=parent-id] query selector in your test returns the parent div and not the dd element you're looking for. You need to change the selector and or the markup to select the right node. You're not clearing mocks between tests. Add jest.clearAllMocks(); to the afterEach function.


1

Most records in an org are not visible to tests. You need to insert your own records in your test. Why? This makes tests self-contained rather than them relying on pre-existing data. It also usually makes assertions easier to write because the data values added are close at hand in the test. See e.g. Create Test Data for Apex Tests.


0

I did finally like that: global class Animals implements Schedulable { **public static String CRON_EXP = '0 0 * * * ? *';** global void execute(SchedulableContext SC) { Selector.getAnimals(); Calculator.setAnimals(); } } Adding this: @isTest static void testAnimals() { Test.startTest(); String jobId = System....


1

The global modifier doesn't change the fact that your execute() method is still an instance method. ClassName.method() is how static methods are called. Since execute() doesn't have the static modifier (and I don't think it should have that modifier anyway), you'll need to create an instance of your class to be able to call the instance method. e.g. ...


0

I guess you are trying to (functionally) test a trigger which invokes the Methods in MonthlyForecast class. If that is the case, check if: The trigger is set for (either before or after) update The trigger has a static variable preventing recursive execution in which case the updates won't fire the trigger again. If that is the case, you can either add a ...


0

I had the exact same problem, even flagging (SeeAllData=false) explicitly. It appears that even in test cases SF validates the SKU across all product records. I got around it in my test classes by ensuring the product SKU was guaranteed to be unique & not match anything in an existing product record (in my case I appended 'SF-UNITTEST-' to all SKUs ...


0

The problem was the importance of applying the System.assertEquals() method which ensured the code coverage. Thank you for your support. https://trailhead.salesforce.com/en/content/learn/modules/asynchronous_apex/async_apex_future_methods


0

Since the properties of this class are not in the official salesforce documentation, hopefully this helps someone. If you need to create an instance of this that return false for isSuccess() Messaging.SendEmailResult failedMessageResult = (Messaging.SendEmailResult)JSON.deserialize('{"success":false}', Messaging.SendEmailResult.class);


5

Nor average, neither sum of percents. If some test methods cover one line of code twice, it only means that this exact line of code is covered. So the actual test coverage is linesCovered / allLines * 100. Example: imagine 1st test class covers one method in your controller (30%), 2nd test class covers 2 methods including the previous mentioned one (50%), ...


2

Set up records that need sharing in @testSetup. The share records will be visible for all unit tests after @testSetup completes. By using @testSetup, things that occur "after the transaction", such as record sharing, are created. Tests that use those records will then have access to those records. Confirmation @isTest class q293720 { @testSetup ...


4

You have to query back the opportunity in order to see the value of the field. Since the value you are showing on the debug shows the value contained in the variable testOpportunity and not the value of the opportunity stored in memory(which was modified by the trigger). add a line after the update to update the value of the variable with the value stored ...


2

Hello it's because you did incorrect import of the component. In component definition you have export default ..., so you need to import import LWCpractice from 'c/lWCPractice'; instead of import { LWCpractice } from 'c/lWCPractice'; try this import { createElement } from "lwc"; import LWCpractice from 'c/lWCPractice'; describe("component 1", () => { ...


1

Your cache is an entirely separate mechanism. The passthrough itself would just grab the appropriate collection. protected override List<SObject> passthrough(List<SObject> records) { return cache.get(records.getSObjectType()); } Managing the cache itself isn't all that complicated. @IsTest public with sharing class QueryMock extends ...


0

@vamsi-krishna is right. You need to add Replacement_Part__c =true for your Product2 //create test objects Product2 testEquipment1 = new Product2(Name='Test Equipment 1', Lifespan_Months__c=12, Replacement_Part__c =true); Product2 testEquipment2 = new Product2(Name='Test ...


1

The JSON syntax for a list or array of strings (IDs are strings) is: ["string", "string", "string"] etc. The syntax "{...}" represents an object, which is different to an array. To simplify your case, I suggest your test set up an actual list and serialize to JSON, since this will automatically use the correct syntax. E.g.: Map<Id, SObject> ...


0

Completely generic exception handlers that catch any exception and "handle" it with a System.debug() are virtually never a good solution. These handlers run the risk of corrupting the data in your database because you're quite literally suppressing the natural effect of an "exceptional" error situation: the rollback of the transaction. The best solutions ...


0

Without knowing the contents of method2, I'd have to say... You can't possibly throw an exception here. The catch block can't be entered, as there's nothing here that can cause an exception. Also: if (SObject.size() > 0) { This is pointless, as iterating over an empty loop is legal, and a query will never return a null value. You don't need it. You'd ...


-1

After playing around with it a bit, I found the following worked for me: ALL DML and *queries* (I found this necessary, as well) goes here Test.startTest(); // before Test.setMock Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator()); client.bar(foo.Id) Test.stopTest();


2

Are setup objects accessible in test classes regardless of SeeAllData attribute's value? Yes. A wide variety of setup objects are available in test context, as documented here: However, objects that are used to manage your organization or metadata objects can still be accessed in your tests such as: User Profile Organization AsyncApexJob ...


1

The first things to look for when a trigger is not receiving coverage are Is the test passing? Is the trigger being invoked by performing the correct DML operation? Is the trigger active? Per your comment, this time the issue was that the trigger was inactive.


2

I think that perhaps you can take an approach a bit more user friendly.. just a though. Giving the users the Insufficient privileges error. is not really a great experience. You can use your Constants Helper Class, if you have one... it not, is a good reason to start building one? Lest say you have your constants class with something like this: public ...


0

For a test class, I like to simply upsert: CustSettings__c settings = new custSettings__c.getInstance(); settings.someField__c = 'Some Value'; upsert settings; The advantage being, is generally in a test class you don't care what the hierarchy is, you just care about what values your test class will be using. If you want to test with a profile instead, ...


Top 50 recent answers are included