You need to sign in to do that
Don't have an account?
Alex Kirby
Covering Maps in a test class
Hi All,
I have recently started using maps in SF and I am struggling to work out test coverage. I managed to complete the same operation using SOQL but I waned to reduce the amount used in the org.
Here is my trigger:
Here is my test class:
This gives me 71% coverage but I can seem to cover any of the following lines: 23 / 28 / 31 and so on.
Thanks,
A
I have recently started using maps in SF and I am struggling to work out test coverage. I managed to complete the same operation using SOQL but I waned to reduce the amount used in the org.
Here is my trigger:
trigger Net_Mar_Ini on Net_Margin_Calculation__c (before insert) { string index; Map<String, Double> mapIndexToCost = new Map<String, Double>(); set<String> setcollected = new Set<String>(); for(Net_Margin_Calculation__c NM : Trigger.new){ index = nm.index__c; } setcollected.add(index + 'Acquire'); setcollected.add(index + 'Bad Debt'); setcollected.add(index + 'Debt Admin'); setcollected.add(index + 'Lose'); setcollected.add(index + 'Meter'); setcollected.add(index + 'Other'); setcollected.add(index + 'Register'); setcollected.add(index + 'Renew'); setcollected.add(index + 'Serve'); setcollected.add(index + 'Support'); for(Cost__c objCost : [select Index__C, Value__c FROM Cost__c WHERE Index__c IN : setcollected]){ mapIndexToCost.put(objCost.Index__c, double.valueof(objCost.Value__c));} for(Net_Margin_Calculation__c NM : Trigger.new){ if(mapIndexToCost.containskey(nm.index__c + 'Acquire')){ NM.Acquire_MPAN__c = Double.valueOf(mapIndexToCost.get(nm.index__c + 'Acquire'));} if(mapIndexToCost.containskey(nm.index__c + 'Bad Debt')){ NM.Bad_Debt_MWh__c = Double.valueOf(mapIndexToCost.get(nm.index__c + 'Bad Debt'));} if(mapIndexToCost.containskey(nm.index__c + 'Debt Admin')){ NM.Debt_Admin_MWh__c = Double.valueOf(mapIndexToCost.get(nm.index__c + 'Debt Admin'));} if(mapIndexToCost.containskey(nm.index__c + 'Lose')){ NM.Lose_MPAN__c = Double.valueOf(mapIndexToCost.get(nm.index__c + 'Lose'));} if(mapIndexToCost.containskey(nm.index__c + 'Meter')){ NM.Meter_MPAN_Annum__c = Double.valueOf(mapIndexToCost.get(nm.index__c + 'Meter'));} if(mapIndexToCost.containskey(nm.index__c + 'Other')){ NM.Other_MWh__c = Double.valueOf(mapIndexToCost.get(nm.index__c + 'Other'));} if(mapIndexToCost.containskey(nm.index__c + 'Register')){ NM.Register_MPAN__c = Double.valueOf(mapIndexToCost.get(nm.index__c + 'Register'));} if(mapIndexToCost.containskey(nm.index__c + 'Renew')){ NM.Renew_MPAN__c = Double.valueOf(mapIndexToCost.get(nm.index__c + 'Renew'));} if(mapIndexToCost.containskey(nm.index__c + 'Serve')){ NM.Serve_MPAN_Annum__c = Double.valueOf(mapIndexToCost.get(nm.index__c + 'Serve'));} if(mapIndexToCost.containskey(nm.index__c + 'Support')){ NM.Support_MWh__c = Double.valueOf(mapIndexToCost.get(nm.index__c + 'Support')); } } }
Here is my test class:
@isTest public class Test_eBit { static testMethod void myUnitTest() { CreateAccOpp.CreateOppty(); List<Opportunity> opps = [SELECT id FROM Opportunity]; List<Credit_check__c> ccs = [Select id, opportunity__c, opportunity_2__c, opportunity_3__c from credit_check__c]; Id Opp = opps.get(0).Id; Id ccID = ccs.get(0).Id; Net_Margin_Calculation__c NM = new Net_Margin_Calculation__c(); nm.Opportunity_Name__c = opp; Insert NM; Cost__c c = new Cost__c(); c.Fuel__c = 'Power'; c.product__c = 'FIXED'; c.channel__c = 'Acquisition - Direct Versus Indirect'; c.category__c = 'Acquire'; c.value__c = 1.00; Insert c; Opportunity Op = new Opportunity(); Op.id = Opp; Op.Annual_Volume_GWh__c = 7; Op.Number_of_Meters__c = 1; Op.Sales_Channel__c = 'Business Movers'; Op.Product_Type__c = 'Supply (Power NHH)'; Op.Type = 'Existing Business'; Op.Start_Date__c = system.today()+1; Op.End_Date__c = system.today()+366; Op.Estimated_Margin_MWh__c = 2; Update Op; } }
This gives me 71% coverage but I can seem to cover any of the following lines: 23 / 28 / 31 and so on.
Thanks,
A
The index value is a formulae, here is some debug information giving the index value:
Part of test class below:
15:17:17.069 (3069589624)|USER_DEBUG|[35]|DEBUG|Cost Index Power-FIXED-Acquisition - Direct Versus Indirect-Serve 15:17:17.069 (3069596437)|SYSTEM_METHOD_EXIT|[35]|System.debug(ANY) 15:17:17.069 (3069605283)|SYSTEM_METHOD_ENTRY|[34]|system.ListIterator.hasNext() 15:17:17.069 (3069616142)|SYSTEM_METHOD_EXIT|[34]|system.ListIterator.hasNext() 15:17:17.069 (3069647887)|SYSTEM_METHOD_ENTRY|[35]|System.debug(ANY) 15:17:17.069 (3069653927)|USER_DEBUG|[35]|DEBUG|Cost Index Power-FIXED-Acquisition - Direct Versus Indirect-Meter 15:17:17.069 (3069659991)|SYSTEM_METHOD_EXIT|[35]|System.debug(ANY) 15:17:17.069 (3069667339)|SYSTEM_METHOD_ENTRY|[34]|system.ListIterator.hasNext() 15:17:17.069 (3069676690)|SYSTEM_METHOD_EXIT|[34]|system.ListIterator.hasNext() 15:17:17.069 (3069704403)|SYSTEM_METHOD_ENTRY|[35]|System.debug(ANY) 15:17:17.069 (3069709881)|USER_DEBUG|[35]|DEBUG|Cost Index Power-FIXED-Acquisition - Direct Versus Indirect-Lose 15:17:17.069 (3069714104)|SYSTEM_METHOD_EXIT|[35]|
It would apprear the the index is being created correctly.
All Answers
The reason you are having this problem is that when the trigger is called, there are no Cost__c in the system. So the query on line 22 will not return records, therefore the map will be empty, therefore 28,31,etc will never be hit.
To fix this, move the creation of the Cost__c in the test class to before the creation of the Net_Margin_Calculation__c (Line 13 will do). You will also have to create more than one cost to cover all the different options that you check for in your trigger.
Here is how I would write the test class. It should have higher coverage:
Thank you for the help, unfortunatley I am still getting the same 71% coverage. It could be a process problem, I'll have a chat with another developer my end and post back.
There is no use of updating the opportunity because your trigger is on Net_Margin_Calculation__c object..
Another thing is you need to insert 10 Net_Margin_Calculation__c and 10 cost records where index value will be same so it will go inside all the if condition..
Another things is , in your current code you are inserting the Net_Margin_Calculation__c record but you have not provided the index value..alos while inserting the cost record also you should be provideing teh same index value so it can cover
Please insert all these records properly so it will cover 100%
Also , let me know if you need any help on this..
Example
//Insert this object with all mandatory fields
Net_Margin_Calculation__c objNM = new Net_Margin_Calculation__c (Index__c = 1);
insert objNM;
//insert cost record
Cost__c c = new Cost__c();
c.Fuel__c = 'Power';
c.product__c = 'FIXED';
c.channel__c = 'Acquisition - Direct Versus Indirect';
c.category__c = 'Acquire';
c.value__c = 1.00;
c.Index__c = 1Acquire;
Insert c;
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thanks,
Sandeep
Salesforce Certified Developer
The NetMargin record is generating automatically on opportunity update. I know I need to cover off the if statements in the net margin loop at the minute I am struggling at covering the following:
for(Cost__c objCost : [select Index__C, Value__c FROM Cost__c WHERE Index__c IN : setcollected]){
mapIndexToCost.put(objCost.Index__c, double.valueof(objCost.Value__c));}
This is a static table where values are pulled back from. The index in the NM record is a formula using fields from Opportunity.
Are you able to insert Cost record with Index value ?
The index value is a formulae, here is some debug information giving the index value:
Part of test class below:
15:17:17.069 (3069589624)|USER_DEBUG|[35]|DEBUG|Cost Index Power-FIXED-Acquisition - Direct Versus Indirect-Serve 15:17:17.069 (3069596437)|SYSTEM_METHOD_EXIT|[35]|System.debug(ANY) 15:17:17.069 (3069605283)|SYSTEM_METHOD_ENTRY|[34]|system.ListIterator.hasNext() 15:17:17.069 (3069616142)|SYSTEM_METHOD_EXIT|[34]|system.ListIterator.hasNext() 15:17:17.069 (3069647887)|SYSTEM_METHOD_ENTRY|[35]|System.debug(ANY) 15:17:17.069 (3069653927)|USER_DEBUG|[35]|DEBUG|Cost Index Power-FIXED-Acquisition - Direct Versus Indirect-Meter 15:17:17.069 (3069659991)|SYSTEM_METHOD_EXIT|[35]|System.debug(ANY) 15:17:17.069 (3069667339)|SYSTEM_METHOD_ENTRY|[34]|system.ListIterator.hasNext() 15:17:17.069 (3069676690)|SYSTEM_METHOD_EXIT|[34]|system.ListIterator.hasNext() 15:17:17.069 (3069704403)|SYSTEM_METHOD_ENTRY|[35]|System.debug(ANY) 15:17:17.069 (3069709881)|USER_DEBUG|[35]|DEBUG|Cost Index Power-FIXED-Acquisition - Direct Versus Indirect-Lose 15:17:17.069 (3069714104)|SYSTEM_METHOD_EXIT|[35]|
It would apprear the the index is being created correctly.