You need to sign in to do that
Don't have an account?
sonam guptha
Code coverage issue for triggerhandler
Hi,
Can anyone help me to fix this issue,here am attaching the test class which am able to cover 50%,just help me out to cover the rest.
Thanks
Can anyone help me to fix this issue,here am attaching the test class which am able to cover 50%,just help me out to cover the rest.
//trigger code trigger UpdateTotalAmount on Opportunity (after insert,after update) { clsUpdateAccount.updateAccount(Trigger.new); } //APEX Class public class clsUpdateAccount{ public static void updateAccount(List<Opportunity> lstOpportunities) { List<Id> lstAccountIDs = new List<Id>(); for(Opportunity opp: lstOpportunities) { lstAccountIDs.add(opp.AccountId); } Map<Id, Account> mapAccount = new Map<Id, Account>([SELECT Id, Name, TotalAmount__c FROM Account WHERE Id =:lstAccountIDs]); List<Account> lstAccounts = new List<Account>(); for(AggregateResult result: [SELECT SUM(Amount) Amt, AccountId FROM Opportunity GROUP BY AccountId, StageName Having AccountId IN :mapAccount.keyset() AND StageName = 'Closed Won' ]) { Account acc = mapAccount.get((Id)result.get('AccountID')); acc.TotalAmount__c = (Decimal)result.get('Amt'); lstAccounts.add(acc); } if(lstAccounts.size() > 0){ update lstAccounts; } } }
static testmethod void oppamount(){ Test.starttest(); Account acc = new Account(Name = 'testacc' ,TotalAmount__c = 1000); insert acc; Opportunity opp = new Opportunity(RecordTypeId = '012E0000000Dke9', Name = 'name', CloseDate = Date.today(), StageName = 'Prospecting', AccountId = acc.id, Amount = 1000, ForecastCategoryName = 'nextlevel', Opportunity_Source__c='Marketing Campaign (Inbound)'); insert opp; Account acc1 = [SELECT id, Sum_of_Opportunity_Amount__c FROM Account WHERE Id = :acc.Id]; // Verification System.assertEquals(acc1.TotalAmount__c, opp.amount); Test.stopTest(); }But here am getting below error:--
System.AssertException: Assertion Failed: Expected: null, Actual: 1000
Thanks
I just relised the problem. It seems the query you used is not correctly done .Kindly use the below Query and check.
Now coming to the test coverage for the formula field .We cant hardcoding the formula field in test class.As I can see you used the formula field opp_priority in the query when its value is true , you need to do the same for the test class also , like you need to crete the opportunity record like that after insert or update the formula field opp_priority becomes true.Let me give you my scenario, it will help you to understand. I created a same opp_priority__c formula checkbox field in the opportunity object and this field will be checked (true) when the oppotunity record will be created/updated with stage name as 'Closed Won' .So As you can see in my test class while creating /updating the opportunity record I made the stageName field set as closed won so that after insert /update for that record opp_priority__c field becomes automatically true. I dont know what scenario you used for that formula field but during testing you need to test like this.I mean u need to assign those field values so that for the test opportunity record the opp_priority__c field becomes true.Now coming to the test class ,there is no change for the test class for my scenario, only I used the same opportunity name twice :P ,you can change that in your class. Let me know if you have still some problem.
All Answers
Try the below code and let us know if this works for you.
Still am seeing the same error. And line 17 on my triggerhandler i changed the where condition from StageName = 'Closed Won' to opp_priority__c(Formula Field) = true,this may affect in any way,please suggest some solution.
Thanks for the quick response!
This is really Strange .This is working fine in my org.Can u just check and let us know if the trigger is still active? If this not kindly check the active checkbox for the trigger and make the trigger active in the org.Run the test class after that. Thanks!!!
Trigger is Active and covering 100%,but the problem is with triggerhandler,i think i need to include the formula field opp_priority__c = true,but its not allowing me to place that field on test class,it keep on throwing an error,how can i write formula field in test class? I used system.asserNOTEquals()?its passing the test class but code coverage is not improving?
I just relised the problem. It seems the query you used is not correctly done .Kindly use the below Query and check.
Now coming to the test coverage for the formula field .We cant hardcoding the formula field in test class.As I can see you used the formula field opp_priority in the query when its value is true , you need to do the same for the test class also , like you need to crete the opportunity record like that after insert or update the formula field opp_priority becomes true.Let me give you my scenario, it will help you to understand. I created a same opp_priority__c formula checkbox field in the opportunity object and this field will be checked (true) when the oppotunity record will be created/updated with stage name as 'Closed Won' .So As you can see in my test class while creating /updating the opportunity record I made the stageName field set as closed won so that after insert /update for that record opp_priority__c field becomes automatically true. I dont know what scenario you used for that formula field but during testing you need to test like this.I mean u need to assign those field values so that for the test opportunity record the opp_priority__c field becomes true.Now coming to the test class ,there is no change for the test class for my scenario, only I used the same opportunity name twice :P ,you can change that in your class. Let me know if you have still some problem.