You need to sign in to do that
Don't have an account?
Maciej Gunia 8
Test class - constructor not defined
I have a trigger which works in the sandbox. The workflow checks the field in the campaign level and compares it with the custom setting. If it matches, then it returns the target to the DS Multiplier field. The trigger looks as follows
However, I had problems to write a proper test to this and asked for the help on the internet. I received the test
Such test returns the error "Compile Error: Variable does not exist: apexCalculatorUserId at line 6 column 122". If I remove that ApexCalculator part System.assertEquals then the test passes. However it covers 4/6 part of the code (which is 66%).
As I was able to find out that apexCalculatorUserId has not been defined. I was suggested to look at the constructor DSTargets__c and see what kind of ID it is expecting there. However, my knowledge doesn't allow me to amend it this way it will work. Would any of you help me to amend it as it seems I am on the last corner before the finish line and am hopeless with this test?
Regards,
Maciej
trigger PopulateTarget on Campaign (before insert, before update) { for(Campaign campaign : Trigger.new) { if (String.isNotBlank(campaign.Apex_Calculator__c) == true) { DSTargets__c targetInstance = DSTargets__c.getInstance(campaign.Apex_Calculator__c); { String target = targetInstance .Target__c; campaign.DS_Target_Multiplier__c = Target; } } } }
However, I had problems to write a proper test to this and asked for the help on the internet. I received the test
@isTest private class testPopulateTarget{ static testMethod void testMethod1(){ // Load the Custom Settings DSTargets__c testSetting = new DSTargets__c(Name='Africa - 10 Weeks; CW 10',Target__c='0.1538', SetupOwnerId = apexCalculatorUserId); insert testSetting; // Create Campaign. Since it would execute trigger, put it in start and stoptests Test.startTest(); Campaign testCamp = new Campaign(); // populate all reqd. fields. testCamp.Name = 'test DS campaign'; testCamp.RecordTypeId = '012200000001b3v'; testCamp.Started_Campaign_weeks_before_Event__c = '12 Weeks'; testCamp.ParentId= '701g0000000EZRk'; insert testCamp; Test.stopTest(); testCamp = [Select ID,Apex_Calculator__c,DS_Target_Multiplier__c from Campaign where Id = :testCamp.Id]; system.assertEquals(testCamp.DS_Target_Multiplier__c,testSetting.Target__c);// assert that target is populated right } }
Such test returns the error "Compile Error: Variable does not exist: apexCalculatorUserId at line 6 column 122". If I remove that ApexCalculator part System.assertEquals then the test passes. However it covers 4/6 part of the code (which is 66%).
As I was able to find out that apexCalculatorUserId has not been defined. I was suggested to look at the constructor DSTargets__c and see what kind of ID it is expecting there. However, my knowledge doesn't allow me to amend it this way it will work. Would any of you help me to amend it as it seems I am on the last corner before the finish line and am hopeless with this test?
Regards,
Maciej
Nitesh
When I change SetupOwnerId = apexCalculatorUserId to SetupOwnerId = UserInfo.getUserId() I get the following error message
I also tried to change SetupOwnerId = apexCalculatorUserId to SetupOwnerId = :apexCalculatorUserId but then I can't save this test class as it says
I am not fluent with apex coding. Therefore, I would like to ask if some of you could help me to define the variable appropriately?
Regards,
Maciej
SetupOwnerId = UserInfo.getProfileId()
I've already tried that and as I wrote above that returns the following error message while running the test:
If yes, that try this one
SetupOwnerId = [Select Id from Profile where In != UserInfo.getProfileId() Limit 1].Id
or
DSTargets__c testSetting = new DSTargets__c(Name='Africa - 10 Weeks; CW 10',Target__c='0.1538', SetupOwnerId = UserInfo.getProfileId());
or
DSTargets__c testSetting = new DSTargets__c(Name='Africa - 10 Weeks; CW 10',Target__c='0.1538', SetupOwnerId = [Select Id from Profile where Id != UserInfo.getProfileId() limit 1].Id);
Try these one by one.
Third one doesn't let me to save the class returning the error: Error: Compile Error: expecting a colon, found 'UserInfo.getProfileId' at line 6 column 153. If I add the colon before UserInfo.getProfileID(), then it fails with the same error message as the two ones before
DSTargets__c testSetting = new DSTargets__c.getOrgDefaults();
testSetting.Name='Africa - 10 Weeks; CW 10';
testSetting.Target__c='0.1538';
insert testSetting;