You need to sign in to do that
Don't have an account?
Bablu Kumar Pandit
How to write test class for Below i write but not code covarge
//-----Handler Class------// public class OppPrintCampaign{ public static void InsertOpp(List<Opportunity> lstopp,Map<Id,Opportunity> mapIdToSatge){ Set<Id> setOfAccId = New Set<Id>(); List<Account> lstupdateacc = New List<Account>(); List<Opportunity> lstofClosedLostOpp = New List<Opportunity>(); Campaign objcamp = [Select Id,name from campaign where Id = '7012v000002KBp9']; for(Opportunity objopp:lstopp){ if(objopp.accountId != null && objopp.stageName == 'Closed Lost' && objopp.StageName != mapIdToSatge.get(objopp.Id).stageName){ setOfAccId.add(objopp.accountId); } } System.debug('-----setOfAccId-----'+setOfAccId); for(Opportunity objopp :[Select Id,Name ,Stagename ,AccountId from Opportunity where AccountId IN:setOfAccId]){ if(objopp.StageName != 'Closed Lost'){ lstofClosedLostOpp.add(objopp); break; } } System.debug('----lstofClosedLostOpp----'+lstofClosedLostOpp.size()); if(lstofClosedLostOpp.size() == 0){ for(Account objacc:[Select Id,Name from Account where Id IN:setOfAccId]){ objacc.Assign_Engments__c = objcamp.name; lstupdateacc.add(objacc); } } System.debug('-----lstupdateacc-----'+lstupdateacc); update lstupdateacc; } } //------Test Class-----// @isTest public class OppPrintCampaignTest { static testmethod void method1(){ Account objacc = new Account(); objacc.name = 'Test'; objacc.Rating = 'Cold'; insert objacc; Opportunity objopp = New Opportunity(); objopp.name = 'Test opp'; objopp.CloseDate = System.today(); objopp.AccountId = objacc.Id; objopp.StageName = 'Closed Lost'; insert objopp; } }
You indicate that OppPrintCampaign is part of a Handler class. How is it invoked?
Looking at the format of the method I am assuming that lstopp is trigger new and mapIdToSatge is trigger oldMap.
Based on that assumption, you will need to fire an update on your opportunity in the test class as the insert alone will not fire the handler method.
Also your code is checking for a change from Open to Closed Lost, so insert with a different stage.
Regards
Andrew
p.s. all code provided uncompiled and as-is
your assumption is correct trigger fire on Opportunity in which lstopp whcih has New List of trigger and oldmap is old value of trigger
i am tried above code but not cover covrage is 29%
Please guide how to increacse covrage
i have simulated that code in a dev environment and I can achieve 86% coverage.
I suspect the issue is with the Campaign not being returned in you code. The campaign will exist as a record in the live environment, and therefore is not visible to the test class.
So, I would adjust as follows: (note that I don't think Ids should ever be hardcoded into apex class, it makes them less portable.)
Class: Test class
Since we appear worried about checking if the value was updated correctly, lets also do a negative test where the record is not updated.
Note, we also use the @TestSetup convention to setup the test data once.
We have also removed the use of testMethod as that has been deprecated.
ref: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_isTest.htm
The above will give 100% coverage and if your Trigger structure looks like below, it would 100% coverage also:
Note also, i would change the method name, as insertOpp is not really a clear name for what the method is doing.
For a naming convention, my handler would be named opportunityTriggerHandler - some may say not very original, but it means I can add other logic (methods) to the class but the class purpose is still quite obvious.
I would then change the method name to something like setAccountCampaignReference - yes, a little unwieldy, but it is quite obvious what the method is about to do.
That way I could read the in the trigger as: And i would have a clear idea of what was happening without necessarily going to the handler class to find out.
Regards
Andrew