-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
3Questions
-
6Replies
Not Able to do code coverage for the below Apex class
Hello Folks
i am new to the apex development, i m trying to write test class for the below apex class but not able to do the code coverage, Any help will be appreciated
Apex class
My Test class
i am new to the apex development, i m trying to write test class for the below apex class but not able to do the code coverage, Any help will be appreciated
Apex class
public class AggregateApprovalHelper { public static void updateOpportunities(Map<Id,Aggregate_Approval__c> newMap, Map<Id,Aggregate_Approval__c> oldMap){ List<Id> approvedAggregateIds = new List<Id>(); Map<Id, Aggregate_Approval__c> apprMap = new Map<Id, Aggregate_Approval__c>(); for(Id appId : newMap.keySet()){ if(newMap.get(appId).Status__c != oldMap.get(appId).Status__c && (newMap.get(appId).Status__c == 'Approved') || newMap.get(appId).Status__c == 'Rejected'){ approvedAggregateIds.add(appId); apprMap.put(appId,newMap.get(appId)); } } if(apprMap.size()>0){ // BatchUpdateOpportunities bat = new BatchUpdateOpportunities(apprMap); // Database.executeBatch(bat); } if(approvedAggregateIds.size()>0){ List<Opportunity> opps = [Select Id, StageName, Aggregate_Approval__r.Status__c from Opportunity where Aggregate_Approval__c IN :approvedAggregateIds]; List<Opportunity> oppsToBeApproved = new List<Opportunity>(); for(Opportunity o : opps){ o.Status__c = o.Aggregate_Approval__r.Status__c; if(o.Aggregate_Approval__r.Status__c == 'Approved') o.Billing_To_Be_Initiated__c ='Yes'; else if(o.Aggregate_Approval__r.Status__c == 'Rejected') o.Billing_To_Be_Initiated__c ='No'; oppsToBeApproved.add(o); } if(oppsToBeApproved.size()>0){ List<Approval.UnlockResult> ur = Approval.unlock(oppsToBeApproved); update oppsToBeApproved; } } } }
My Test class
@isTest public class AggregateApprovalTestclass { static testmethod void Method(){ List<Aggregate_Approval__c> AggreApp = new list <Aggregate_Approval__c>(); Aggregate_Approval__c Agg = new Aggregate_Approval__c(); Agg.Month__c = 'October'; Agg.Next_Month_Start_Date__c = system.today(); Agg.Status__c = 'Created'; Agg.Year__c = '2020'; AggreApp.add(Agg); insert Agg; } static testmethod void Method1(){ List<Opportunity> Oppps = new list <Opportunity>(); Opportunity oop = new Opportunity(); Account testAcct = new Account (Name = 'My Test Account'); insert testAcct; User u = [Select id,name from user where isactive=true Limit 1]; oop.Name = 'Test1'; oop.AccountId = testAcct.ID; oop.StageName = 'Demo'; oop.CloseDate = system.today(); oop.LeadSource = 'Customer'; oop.Country__c = 'India'; Oppps.add(oop); insert oop; } }
- Janaki Rao
- March 18, 2020
- Like
- 0
How to write Test class for Contact Duplication Trigger
i am writing a test Class to detect duplicate based on 3 custom fields called User__c, Quater_Year__c,Month__c , i have written the bulkified code and invoked the class from a trigger, how do i cover 100%
Handler class
Trigger
My Test class with 50% code coverage
Handler class
public class ContactTriggerHandler { public static void dupContactError (List <Contact> newTrigCon) { Id recordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Contact').getRecordTypeId(); //List of all Contact Records currently in the database List <Contact> allCon = [SELECT ID, Name, User__c, Quater_Year__c,RecordTypeId, Month__c FROM Contact where RecordTypeId=:recordTypeId]; //Iterate through new potential records for (Contact oneConTrig : newTrigCon) { //Needed to iterate through all existing records which is contained in the allCon List for (Integer i = 0; i < allCon.size(); i++) //If all conditions are met, there is a duplicate and thus, returns an error. if (oneContrig.User__c == allCon[i].User__c && oneContrig.Quater_Year__c == allCon[i].Quater_Year__c && oneConTrig.Month__c == allCon[i].Month__c && oneConTrig.RecordTypeId == allCon[i].RecordTypeId) { oneConTrig.addError('Duplicate Contact Error! This record already exists.'); } } } }
Trigger
trigger ContactTrigger on Contact (before insert, before update) { ContactTriggerHandler.dupContactError(Trigger.new); }
My Test class with 50% code coverage
@isTest public class ContactTriggerHandlerTestClass { static testmethod void Duplication(){ List<Contact> Contacts = new list <Contact>(); Id RecordTypeIdLead = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Contact').getRecordTypeId(); Account testAccount = new Account(); testAccount.Name='DCCL' ; insert testAccount; contact con = new contact(); con.FirstName='Julian'; con.LastName='Rogers'; con.Month__c = 'march'; con.Quater_Year__c = '2020'; con.AccountId = testAccount.id; con.Email = 'hello123@gmail.com'; Contacts.add(con); insert con; } }
- Janaki Rao
- February 26, 2020
- Like
- 0
How do to make the code coverage to 100% for the below Test Class
Hello Developers
I am Working on a Test class where it has already covered 96%, how do i make it to 100%, I have Tried all the Best Pratices for the Test class, still i am not able to make it to 100%
This is my Helper Class
public class LeadHelper {
public static List<LeadShare> csShareList = new List<LeadShare>();
public static List<LeadShare> removeShareList = new List<LeadShare>();
public static List<Lead> leadsToAddAccountId = new List<Lead>();
public static List<String> leadCompanyNames = new List<String>();
public static Map<String, Id> matchingAcctNames = new Map<String,Id>();
public static void leadShareInsertUpdate(List<Lead> leads){
csShareList = new List<LeadShare>();
removeShareList = new List<LeadShare>();
Set<Id> ldIds = new Set<Id>();
for( Lead cs : leads) {
if( cs.CRM_User__c != NULL ) {
// Create a new LeadShare object for each Lead where CRM_User__c field is not NULL.
LeadShare csShare = new LeadShare();
// Give Read write access to that user for this particular Lead record.
csShare.LeadAccessLevel = 'edit';
// Assign Lead Id of Lead record.
csShare.LeadId = cs.id;
// Assign user id to grant read write access to this particular Lead record.
csShare.UserOrGroupId = cs.CRM_User__c;
csShareList.add( csShare );
}
if( cs.CRM_User__c == NULL ) {
ldIds.add( cs.id);
}
}
if(ldIds.size()>0){
removeShareList = [Select id,LeadId,UserOrGroupId from LeadShare where LeadId=:ldIds AND RowCause = 'Manual'];
if(removeShareList.size()>0){
delete removeShareList;
}
}
if( csShareList != null && csShareList.size() != 0 ) {
insert csShareList;
update csShareList;
}
}
public static void leadsToAddAccountId(List<Lead> leads){
leadsToAddAccountId = new List<Lead>();
leadCompanyNames = new List<String>();
matchingAcctNames = new Map<String,Id>();
for (Lead newLead : leads){
leadsToAddAccountId.add(newLead);
leadCompanyNames.add(newLead.Company);
}
for(Account acct : [Select Id, Name from Account where Name =: leadCompanyNames]){
matchingAcctNames.put(acct.Name, acct.Id);
}
for(Lead myLead : leadsToAddAccountId ){
myLead.Account__c = matchingAcctNames.get(myLead.Company);
}
}
public static void leadIncrementroundrobin(List<Lead> leads){
Id recordTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Lead Registration').getRecordTypeId();
List<Lead> leadList = [Select Id, Updated_Region__c, RecordTypeId From Lead where RecordTypeId=:recordTypeId ALL ROWS];
Map<String, List<Lead>> countryLeadsMap = new Map<String, List<Lead>>();
for (Lead l : leadList) {
if (!countryLeadsMap.containsKey(l.Updated_Region__c)) {
countryLeadsMap.put(l.Updated_Region__c, new List<Lead>());
}
countryLeadsMap.get(l.Updated_Region__c).add(l);
}
for (Lead nl : leads){
if(nl.RecordTypeId == recordTypeId){
if (!countryLeadsMap.containsKey(nl.Updated_Region__c)) {
countryLeadsMap.put(nl.Updated_Region__c, new List<Lead>());
}
countryLeadsMap.get(nl.Updated_Region__c).add(nl);
if (nl.Updated_Region__c == 'India') {
nl.Lead_No_Region_IN__c = countryLeadsMap.get(nl.Updated_Region__c).size();
}
if (nl.Updated_Region__c == 'US') {
nl.Lead_No_Region_USA__c = countryLeadsMap.get(nl.Updated_Region__c).size();
}
if (nl.Updated_Region__c == 'EU') {
nl.Lead_No_Region_EU__c = countryLeadsMap.get(nl.Updated_Region__c).size();
}
if (nl.Updated_Region__c == 'ME') {
nl.Lead_No_Region_ME__c = countryLeadsMap.get(nl.Updated_Region__c).size();
}
if (nl.Updated_Region__c == 'SEA1') {
nl.Lead_No_Region_SEA1__c = countryLeadsMap.get(nl.Updated_Region__c).size();
}
if (nl.Updated_Region__c == 'SEA2') {
nl.Lead_No_Region_SEA2__c = countryLeadsMap.get(nl.Updated_Region__c).size();
}
}
}
}
}
-----------------------------------
My test class
@isTest(seealldata=true)
private class LeadTrigger_Test {
// test that newly inserted records marked as pubic=true have corresponding shares created
public static testMethod void testAddShares() {
Set<ID> ids = new Set<ID>();
List<Lead> Leads = new List<Lead>();
Id RecordTypeIdLead = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Partner Lead Registration').getRecordTypeId();
User u = [Select id,name from user where isactive=true Limit 1];
for (Integer i=0;i<10;i++)
Leads.add(new Lead(FirstName='First ',LastName='Name '+i,RecordTypeId=RecordTypeIdLead,
Email='email'+i+'@email.com',Company='ABSYZ',CRM_User__c=u.Id));
insert Leads;
// get a set of all new created ids
for (Lead c : Leads)
ids.add(c.id);
// assert that 50 shares were created
List<LeadShare> shares = [select id from LeadShare where
LeadId IN :ids and RowCause = 'Manual'];
}
// insert records and switch them from public = true to public = false
public static testMethod void testUpdateContacts() {
Set<ID> ids = new Set<ID>();
List<Lead> Leads = new List<Lead>();
Id RecordTypeIdLead = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Partner Lead Registration').getRecordTypeId();
User u = [Select id,name from user where isactive=true Limit 1];
for (Integer i=0;i<50;i++)
Leads.add(new Lead(FirstName='First ',LastName='Name '+i,RecordTypeId=RecordTypeIdLead,
Email='email'+i+'@email.com',Company='ABSYZ',CRM_User__c=u.Id));
insert Leads;
for (Lead c : Leads)
ids.add(c.id);
update Leads;
// assert that 0 shares exist
List<LeadShare> shares = [select id from LeadShare where
LeadId IN :ids and RowCause = 'Manual'];
System.assertNotEquals(shares.size(),0);
for (Lead c : Leads)
c.CRM_User__c=u.Id;
update Leads;
// assert that 50 shares were created
shares = [select id from LeadShare where LeadId IN :ids and RowCause = 'Manual'];
System.assertEquals(shares.size(),50);
for (Lead c : Leads)
c.CRM_User__c=u.Id;
update Leads;
// assert that 0 shares exist
shares = [select id from LeadShare where LeadId IN :ids and RowCause = 'Manual'];
System.assertNotEquals(shares.size(),0);
}
public static testMethod void validateLeadAndAccount() {
List<Lead> leadList = new List<Lead>();
Id RecordTypeIdLead = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Lead Registration').getRecordTypeId();
Account acc = new Account();
Acc.Name = 'Test11';
insert acc;
Lead l1 = new Lead ();
l1.LastName = 'Test';
l1.RecordTypeId = RecordTypeIdLead;
l1.Company = 'Test11';
l1.Country = 'India';
l1.Lead_Category__c = 'Churned Customer';
l1.Status ='Enquiry';
leadList.add(l1);
Lead l2 = new Lead();
l2.Country = 'USA';
l2.Lead_Category__c = 'Churned Customer';
l2.Company = 'Test21';
l2.RecordTypeId = RecordTypeIdLead;
l2.LastName = 'testLast21';
l2.Status = 'Enquiry';
leadList.add(l2);
Lead l3 = new Lead();
l3.Country = 'Taiwan';
l3.Lead_Category__c = 'Churned Customer';
l3.Company = 'Test21';
l3.RecordTypeId = RecordTypeIdLead;
l3.LastName = 'testLast21';
l3.Status = 'Enquiry';
leadList.add(l3);
Lead l4 = new Lead();
l4.Country = 'Egypt';
l4.Lead_Category__c = 'Churned Customer';
l4.Company = 'Test21';
l4.RecordTypeId = RecordTypeIdLead;
l4.LastName = 'testLast21';
l4.Status = 'Enquiry';
leadList.add(l4);
Lead l5 = new Lead();
l5.Country = 'Germany';
l5.Lead_Category__c = 'Churned Customer';
l5.Company = 'Test21';
l5.RecordTypeId = RecordTypeIdLead;
l5.LastName = 'testLast21';
l5.Status = 'Enquiry';
leadList.add(l5);
Lead l6 = new Lead();
l6.Country = 'Malaysia';
l6.Lead_Category__c = 'Churned Customer';
l6.Company = 'Test21';
l6.RecordTypeId = RecordTypeIdLead;
l6.LastName = 'testLast21';
l6.Status = 'Enquiry';
leadList.add(l6);
Lead l7 = new Lead();
l7.Country = 'Singapore';
l7.Lead_Category__c = 'Churned Customer';
l7.Company = 'Test21';
l7.RecordTypeId = RecordTypeIdLead;
l7.LastName = 'testLast21';
l7.Status = 'Enquiry';
leadList.add(l7);
Test.startTest();
Insert leadList;
Test.stopTest();
}
}
I am Working on a Test class where it has already covered 96%, how do i make it to 100%, I have Tried all the Best Pratices for the Test class, still i am not able to make it to 100%
This is my Helper Class
public class LeadHelper {
public static List<LeadShare> csShareList = new List<LeadShare>();
public static List<LeadShare> removeShareList = new List<LeadShare>();
public static List<Lead> leadsToAddAccountId = new List<Lead>();
public static List<String> leadCompanyNames = new List<String>();
public static Map<String, Id> matchingAcctNames = new Map<String,Id>();
public static void leadShareInsertUpdate(List<Lead> leads){
csShareList = new List<LeadShare>();
removeShareList = new List<LeadShare>();
Set<Id> ldIds = new Set<Id>();
for( Lead cs : leads) {
if( cs.CRM_User__c != NULL ) {
// Create a new LeadShare object for each Lead where CRM_User__c field is not NULL.
LeadShare csShare = new LeadShare();
// Give Read write access to that user for this particular Lead record.
csShare.LeadAccessLevel = 'edit';
// Assign Lead Id of Lead record.
csShare.LeadId = cs.id;
// Assign user id to grant read write access to this particular Lead record.
csShare.UserOrGroupId = cs.CRM_User__c;
csShareList.add( csShare );
}
if( cs.CRM_User__c == NULL ) {
ldIds.add( cs.id);
}
}
if(ldIds.size()>0){
removeShareList = [Select id,LeadId,UserOrGroupId from LeadShare where LeadId=:ldIds AND RowCause = 'Manual'];
if(removeShareList.size()>0){
delete removeShareList;
}
}
if( csShareList != null && csShareList.size() != 0 ) {
insert csShareList;
update csShareList;
}
}
public static void leadsToAddAccountId(List<Lead> leads){
leadsToAddAccountId = new List<Lead>();
leadCompanyNames = new List<String>();
matchingAcctNames = new Map<String,Id>();
for (Lead newLead : leads){
leadsToAddAccountId.add(newLead);
leadCompanyNames.add(newLead.Company);
}
for(Account acct : [Select Id, Name from Account where Name =: leadCompanyNames]){
matchingAcctNames.put(acct.Name, acct.Id);
}
for(Lead myLead : leadsToAddAccountId ){
myLead.Account__c = matchingAcctNames.get(myLead.Company);
}
}
public static void leadIncrementroundrobin(List<Lead> leads){
Id recordTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Lead Registration').getRecordTypeId();
List<Lead> leadList = [Select Id, Updated_Region__c, RecordTypeId From Lead where RecordTypeId=:recordTypeId ALL ROWS];
Map<String, List<Lead>> countryLeadsMap = new Map<String, List<Lead>>();
for (Lead l : leadList) {
if (!countryLeadsMap.containsKey(l.Updated_Region__c)) {
countryLeadsMap.put(l.Updated_Region__c, new List<Lead>());
}
countryLeadsMap.get(l.Updated_Region__c).add(l);
}
for (Lead nl : leads){
if(nl.RecordTypeId == recordTypeId){
if (!countryLeadsMap.containsKey(nl.Updated_Region__c)) {
countryLeadsMap.put(nl.Updated_Region__c, new List<Lead>());
}
countryLeadsMap.get(nl.Updated_Region__c).add(nl);
if (nl.Updated_Region__c == 'India') {
nl.Lead_No_Region_IN__c = countryLeadsMap.get(nl.Updated_Region__c).size();
}
if (nl.Updated_Region__c == 'US') {
nl.Lead_No_Region_USA__c = countryLeadsMap.get(nl.Updated_Region__c).size();
}
if (nl.Updated_Region__c == 'EU') {
nl.Lead_No_Region_EU__c = countryLeadsMap.get(nl.Updated_Region__c).size();
}
if (nl.Updated_Region__c == 'ME') {
nl.Lead_No_Region_ME__c = countryLeadsMap.get(nl.Updated_Region__c).size();
}
if (nl.Updated_Region__c == 'SEA1') {
nl.Lead_No_Region_SEA1__c = countryLeadsMap.get(nl.Updated_Region__c).size();
}
if (nl.Updated_Region__c == 'SEA2') {
nl.Lead_No_Region_SEA2__c = countryLeadsMap.get(nl.Updated_Region__c).size();
}
}
}
}
}
-----------------------------------
My test class
@isTest(seealldata=true)
private class LeadTrigger_Test {
// test that newly inserted records marked as pubic=true have corresponding shares created
public static testMethod void testAddShares() {
Set<ID> ids = new Set<ID>();
List<Lead> Leads = new List<Lead>();
Id RecordTypeIdLead = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Partner Lead Registration').getRecordTypeId();
User u = [Select id,name from user where isactive=true Limit 1];
for (Integer i=0;i<10;i++)
Leads.add(new Lead(FirstName='First ',LastName='Name '+i,RecordTypeId=RecordTypeIdLead,
Email='email'+i+'@email.com',Company='ABSYZ',CRM_User__c=u.Id));
insert Leads;
// get a set of all new created ids
for (Lead c : Leads)
ids.add(c.id);
// assert that 50 shares were created
List<LeadShare> shares = [select id from LeadShare where
LeadId IN :ids and RowCause = 'Manual'];
}
// insert records and switch them from public = true to public = false
public static testMethod void testUpdateContacts() {
Set<ID> ids = new Set<ID>();
List<Lead> Leads = new List<Lead>();
Id RecordTypeIdLead = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Partner Lead Registration').getRecordTypeId();
User u = [Select id,name from user where isactive=true Limit 1];
for (Integer i=0;i<50;i++)
Leads.add(new Lead(FirstName='First ',LastName='Name '+i,RecordTypeId=RecordTypeIdLead,
Email='email'+i+'@email.com',Company='ABSYZ',CRM_User__c=u.Id));
insert Leads;
for (Lead c : Leads)
ids.add(c.id);
update Leads;
// assert that 0 shares exist
List<LeadShare> shares = [select id from LeadShare where
LeadId IN :ids and RowCause = 'Manual'];
System.assertNotEquals(shares.size(),0);
for (Lead c : Leads)
c.CRM_User__c=u.Id;
update Leads;
// assert that 50 shares were created
shares = [select id from LeadShare where LeadId IN :ids and RowCause = 'Manual'];
System.assertEquals(shares.size(),50);
for (Lead c : Leads)
c.CRM_User__c=u.Id;
update Leads;
// assert that 0 shares exist
shares = [select id from LeadShare where LeadId IN :ids and RowCause = 'Manual'];
System.assertNotEquals(shares.size(),0);
}
public static testMethod void validateLeadAndAccount() {
List<Lead> leadList = new List<Lead>();
Id RecordTypeIdLead = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Lead Registration').getRecordTypeId();
Account acc = new Account();
Acc.Name = 'Test11';
insert acc;
Lead l1 = new Lead ();
l1.LastName = 'Test';
l1.RecordTypeId = RecordTypeIdLead;
l1.Company = 'Test11';
l1.Country = 'India';
l1.Lead_Category__c = 'Churned Customer';
l1.Status ='Enquiry';
leadList.add(l1);
Lead l2 = new Lead();
l2.Country = 'USA';
l2.Lead_Category__c = 'Churned Customer';
l2.Company = 'Test21';
l2.RecordTypeId = RecordTypeIdLead;
l2.LastName = 'testLast21';
l2.Status = 'Enquiry';
leadList.add(l2);
Lead l3 = new Lead();
l3.Country = 'Taiwan';
l3.Lead_Category__c = 'Churned Customer';
l3.Company = 'Test21';
l3.RecordTypeId = RecordTypeIdLead;
l3.LastName = 'testLast21';
l3.Status = 'Enquiry';
leadList.add(l3);
Lead l4 = new Lead();
l4.Country = 'Egypt';
l4.Lead_Category__c = 'Churned Customer';
l4.Company = 'Test21';
l4.RecordTypeId = RecordTypeIdLead;
l4.LastName = 'testLast21';
l4.Status = 'Enquiry';
leadList.add(l4);
Lead l5 = new Lead();
l5.Country = 'Germany';
l5.Lead_Category__c = 'Churned Customer';
l5.Company = 'Test21';
l5.RecordTypeId = RecordTypeIdLead;
l5.LastName = 'testLast21';
l5.Status = 'Enquiry';
leadList.add(l5);
Lead l6 = new Lead();
l6.Country = 'Malaysia';
l6.Lead_Category__c = 'Churned Customer';
l6.Company = 'Test21';
l6.RecordTypeId = RecordTypeIdLead;
l6.LastName = 'testLast21';
l6.Status = 'Enquiry';
leadList.add(l6);
Lead l7 = new Lead();
l7.Country = 'Singapore';
l7.Lead_Category__c = 'Churned Customer';
l7.Company = 'Test21';
l7.RecordTypeId = RecordTypeIdLead;
l7.LastName = 'testLast21';
l7.Status = 'Enquiry';
leadList.add(l7);
Test.startTest();
Insert leadList;
Test.stopTest();
}
}
- Janaki Rao
- February 12, 2020
- Like
- 0
Not Able to do code coverage for the below Apex class
Hello Folks
i am new to the apex development, i m trying to write test class for the below apex class but not able to do the code coverage, Any help will be appreciated
Apex class
My Test class
i am new to the apex development, i m trying to write test class for the below apex class but not able to do the code coverage, Any help will be appreciated
Apex class
public class AggregateApprovalHelper { public static void updateOpportunities(Map<Id,Aggregate_Approval__c> newMap, Map<Id,Aggregate_Approval__c> oldMap){ List<Id> approvedAggregateIds = new List<Id>(); Map<Id, Aggregate_Approval__c> apprMap = new Map<Id, Aggregate_Approval__c>(); for(Id appId : newMap.keySet()){ if(newMap.get(appId).Status__c != oldMap.get(appId).Status__c && (newMap.get(appId).Status__c == 'Approved') || newMap.get(appId).Status__c == 'Rejected'){ approvedAggregateIds.add(appId); apprMap.put(appId,newMap.get(appId)); } } if(apprMap.size()>0){ // BatchUpdateOpportunities bat = new BatchUpdateOpportunities(apprMap); // Database.executeBatch(bat); } if(approvedAggregateIds.size()>0){ List<Opportunity> opps = [Select Id, StageName, Aggregate_Approval__r.Status__c from Opportunity where Aggregate_Approval__c IN :approvedAggregateIds]; List<Opportunity> oppsToBeApproved = new List<Opportunity>(); for(Opportunity o : opps){ o.Status__c = o.Aggregate_Approval__r.Status__c; if(o.Aggregate_Approval__r.Status__c == 'Approved') o.Billing_To_Be_Initiated__c ='Yes'; else if(o.Aggregate_Approval__r.Status__c == 'Rejected') o.Billing_To_Be_Initiated__c ='No'; oppsToBeApproved.add(o); } if(oppsToBeApproved.size()>0){ List<Approval.UnlockResult> ur = Approval.unlock(oppsToBeApproved); update oppsToBeApproved; } } } }
My Test class
@isTest public class AggregateApprovalTestclass { static testmethod void Method(){ List<Aggregate_Approval__c> AggreApp = new list <Aggregate_Approval__c>(); Aggregate_Approval__c Agg = new Aggregate_Approval__c(); Agg.Month__c = 'October'; Agg.Next_Month_Start_Date__c = system.today(); Agg.Status__c = 'Created'; Agg.Year__c = '2020'; AggreApp.add(Agg); insert Agg; } static testmethod void Method1(){ List<Opportunity> Oppps = new list <Opportunity>(); Opportunity oop = new Opportunity(); Account testAcct = new Account (Name = 'My Test Account'); insert testAcct; User u = [Select id,name from user where isactive=true Limit 1]; oop.Name = 'Test1'; oop.AccountId = testAcct.ID; oop.StageName = 'Demo'; oop.CloseDate = system.today(); oop.LeadSource = 'Customer'; oop.Country__c = 'India'; Oppps.add(oop); insert oop; } }
- Janaki Rao
- March 18, 2020
- Like
- 0
How to write Test class for Contact Duplication Trigger
i am writing a test Class to detect duplicate based on 3 custom fields called User__c, Quater_Year__c,Month__c , i have written the bulkified code and invoked the class from a trigger, how do i cover 100%
Handler class
Trigger
My Test class with 50% code coverage
Handler class
public class ContactTriggerHandler { public static void dupContactError (List <Contact> newTrigCon) { Id recordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Contact').getRecordTypeId(); //List of all Contact Records currently in the database List <Contact> allCon = [SELECT ID, Name, User__c, Quater_Year__c,RecordTypeId, Month__c FROM Contact where RecordTypeId=:recordTypeId]; //Iterate through new potential records for (Contact oneConTrig : newTrigCon) { //Needed to iterate through all existing records which is contained in the allCon List for (Integer i = 0; i < allCon.size(); i++) //If all conditions are met, there is a duplicate and thus, returns an error. if (oneContrig.User__c == allCon[i].User__c && oneContrig.Quater_Year__c == allCon[i].Quater_Year__c && oneConTrig.Month__c == allCon[i].Month__c && oneConTrig.RecordTypeId == allCon[i].RecordTypeId) { oneConTrig.addError('Duplicate Contact Error! This record already exists.'); } } } }
Trigger
trigger ContactTrigger on Contact (before insert, before update) { ContactTriggerHandler.dupContactError(Trigger.new); }
My Test class with 50% code coverage
@isTest public class ContactTriggerHandlerTestClass { static testmethod void Duplication(){ List<Contact> Contacts = new list <Contact>(); Id RecordTypeIdLead = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Contact').getRecordTypeId(); Account testAccount = new Account(); testAccount.Name='DCCL' ; insert testAccount; contact con = new contact(); con.FirstName='Julian'; con.LastName='Rogers'; con.Month__c = 'march'; con.Quater_Year__c = '2020'; con.AccountId = testAccount.id; con.Email = 'hello123@gmail.com'; Contacts.add(con); insert con; } }
- Janaki Rao
- February 26, 2020
- Like
- 0
How do to make the code coverage to 100% for the below Test Class
Hello Developers
I am Working on a Test class where it has already covered 96%, how do i make it to 100%, I have Tried all the Best Pratices for the Test class, still i am not able to make it to 100%
This is my Helper Class
public class LeadHelper {
public static List<LeadShare> csShareList = new List<LeadShare>();
public static List<LeadShare> removeShareList = new List<LeadShare>();
public static List<Lead> leadsToAddAccountId = new List<Lead>();
public static List<String> leadCompanyNames = new List<String>();
public static Map<String, Id> matchingAcctNames = new Map<String,Id>();
public static void leadShareInsertUpdate(List<Lead> leads){
csShareList = new List<LeadShare>();
removeShareList = new List<LeadShare>();
Set<Id> ldIds = new Set<Id>();
for( Lead cs : leads) {
if( cs.CRM_User__c != NULL ) {
// Create a new LeadShare object for each Lead where CRM_User__c field is not NULL.
LeadShare csShare = new LeadShare();
// Give Read write access to that user for this particular Lead record.
csShare.LeadAccessLevel = 'edit';
// Assign Lead Id of Lead record.
csShare.LeadId = cs.id;
// Assign user id to grant read write access to this particular Lead record.
csShare.UserOrGroupId = cs.CRM_User__c;
csShareList.add( csShare );
}
if( cs.CRM_User__c == NULL ) {
ldIds.add( cs.id);
}
}
if(ldIds.size()>0){
removeShareList = [Select id,LeadId,UserOrGroupId from LeadShare where LeadId=:ldIds AND RowCause = 'Manual'];
if(removeShareList.size()>0){
delete removeShareList;
}
}
if( csShareList != null && csShareList.size() != 0 ) {
insert csShareList;
update csShareList;
}
}
public static void leadsToAddAccountId(List<Lead> leads){
leadsToAddAccountId = new List<Lead>();
leadCompanyNames = new List<String>();
matchingAcctNames = new Map<String,Id>();
for (Lead newLead : leads){
leadsToAddAccountId.add(newLead);
leadCompanyNames.add(newLead.Company);
}
for(Account acct : [Select Id, Name from Account where Name =: leadCompanyNames]){
matchingAcctNames.put(acct.Name, acct.Id);
}
for(Lead myLead : leadsToAddAccountId ){
myLead.Account__c = matchingAcctNames.get(myLead.Company);
}
}
public static void leadIncrementroundrobin(List<Lead> leads){
Id recordTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Lead Registration').getRecordTypeId();
List<Lead> leadList = [Select Id, Updated_Region__c, RecordTypeId From Lead where RecordTypeId=:recordTypeId ALL ROWS];
Map<String, List<Lead>> countryLeadsMap = new Map<String, List<Lead>>();
for (Lead l : leadList) {
if (!countryLeadsMap.containsKey(l.Updated_Region__c)) {
countryLeadsMap.put(l.Updated_Region__c, new List<Lead>());
}
countryLeadsMap.get(l.Updated_Region__c).add(l);
}
for (Lead nl : leads){
if(nl.RecordTypeId == recordTypeId){
if (!countryLeadsMap.containsKey(nl.Updated_Region__c)) {
countryLeadsMap.put(nl.Updated_Region__c, new List<Lead>());
}
countryLeadsMap.get(nl.Updated_Region__c).add(nl);
if (nl.Updated_Region__c == 'India') {
nl.Lead_No_Region_IN__c = countryLeadsMap.get(nl.Updated_Region__c).size();
}
if (nl.Updated_Region__c == 'US') {
nl.Lead_No_Region_USA__c = countryLeadsMap.get(nl.Updated_Region__c).size();
}
if (nl.Updated_Region__c == 'EU') {
nl.Lead_No_Region_EU__c = countryLeadsMap.get(nl.Updated_Region__c).size();
}
if (nl.Updated_Region__c == 'ME') {
nl.Lead_No_Region_ME__c = countryLeadsMap.get(nl.Updated_Region__c).size();
}
if (nl.Updated_Region__c == 'SEA1') {
nl.Lead_No_Region_SEA1__c = countryLeadsMap.get(nl.Updated_Region__c).size();
}
if (nl.Updated_Region__c == 'SEA2') {
nl.Lead_No_Region_SEA2__c = countryLeadsMap.get(nl.Updated_Region__c).size();
}
}
}
}
}
-----------------------------------
My test class
@isTest(seealldata=true)
private class LeadTrigger_Test {
// test that newly inserted records marked as pubic=true have corresponding shares created
public static testMethod void testAddShares() {
Set<ID> ids = new Set<ID>();
List<Lead> Leads = new List<Lead>();
Id RecordTypeIdLead = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Partner Lead Registration').getRecordTypeId();
User u = [Select id,name from user where isactive=true Limit 1];
for (Integer i=0;i<10;i++)
Leads.add(new Lead(FirstName='First ',LastName='Name '+i,RecordTypeId=RecordTypeIdLead,
Email='email'+i+'@email.com',Company='ABSYZ',CRM_User__c=u.Id));
insert Leads;
// get a set of all new created ids
for (Lead c : Leads)
ids.add(c.id);
// assert that 50 shares were created
List<LeadShare> shares = [select id from LeadShare where
LeadId IN :ids and RowCause = 'Manual'];
}
// insert records and switch them from public = true to public = false
public static testMethod void testUpdateContacts() {
Set<ID> ids = new Set<ID>();
List<Lead> Leads = new List<Lead>();
Id RecordTypeIdLead = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Partner Lead Registration').getRecordTypeId();
User u = [Select id,name from user where isactive=true Limit 1];
for (Integer i=0;i<50;i++)
Leads.add(new Lead(FirstName='First ',LastName='Name '+i,RecordTypeId=RecordTypeIdLead,
Email='email'+i+'@email.com',Company='ABSYZ',CRM_User__c=u.Id));
insert Leads;
for (Lead c : Leads)
ids.add(c.id);
update Leads;
// assert that 0 shares exist
List<LeadShare> shares = [select id from LeadShare where
LeadId IN :ids and RowCause = 'Manual'];
System.assertNotEquals(shares.size(),0);
for (Lead c : Leads)
c.CRM_User__c=u.Id;
update Leads;
// assert that 50 shares were created
shares = [select id from LeadShare where LeadId IN :ids and RowCause = 'Manual'];
System.assertEquals(shares.size(),50);
for (Lead c : Leads)
c.CRM_User__c=u.Id;
update Leads;
// assert that 0 shares exist
shares = [select id from LeadShare where LeadId IN :ids and RowCause = 'Manual'];
System.assertNotEquals(shares.size(),0);
}
public static testMethod void validateLeadAndAccount() {
List<Lead> leadList = new List<Lead>();
Id RecordTypeIdLead = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Lead Registration').getRecordTypeId();
Account acc = new Account();
Acc.Name = 'Test11';
insert acc;
Lead l1 = new Lead ();
l1.LastName = 'Test';
l1.RecordTypeId = RecordTypeIdLead;
l1.Company = 'Test11';
l1.Country = 'India';
l1.Lead_Category__c = 'Churned Customer';
l1.Status ='Enquiry';
leadList.add(l1);
Lead l2 = new Lead();
l2.Country = 'USA';
l2.Lead_Category__c = 'Churned Customer';
l2.Company = 'Test21';
l2.RecordTypeId = RecordTypeIdLead;
l2.LastName = 'testLast21';
l2.Status = 'Enquiry';
leadList.add(l2);
Lead l3 = new Lead();
l3.Country = 'Taiwan';
l3.Lead_Category__c = 'Churned Customer';
l3.Company = 'Test21';
l3.RecordTypeId = RecordTypeIdLead;
l3.LastName = 'testLast21';
l3.Status = 'Enquiry';
leadList.add(l3);
Lead l4 = new Lead();
l4.Country = 'Egypt';
l4.Lead_Category__c = 'Churned Customer';
l4.Company = 'Test21';
l4.RecordTypeId = RecordTypeIdLead;
l4.LastName = 'testLast21';
l4.Status = 'Enquiry';
leadList.add(l4);
Lead l5 = new Lead();
l5.Country = 'Germany';
l5.Lead_Category__c = 'Churned Customer';
l5.Company = 'Test21';
l5.RecordTypeId = RecordTypeIdLead;
l5.LastName = 'testLast21';
l5.Status = 'Enquiry';
leadList.add(l5);
Lead l6 = new Lead();
l6.Country = 'Malaysia';
l6.Lead_Category__c = 'Churned Customer';
l6.Company = 'Test21';
l6.RecordTypeId = RecordTypeIdLead;
l6.LastName = 'testLast21';
l6.Status = 'Enquiry';
leadList.add(l6);
Lead l7 = new Lead();
l7.Country = 'Singapore';
l7.Lead_Category__c = 'Churned Customer';
l7.Company = 'Test21';
l7.RecordTypeId = RecordTypeIdLead;
l7.LastName = 'testLast21';
l7.Status = 'Enquiry';
leadList.add(l7);
Test.startTest();
Insert leadList;
Test.stopTest();
}
}
I am Working on a Test class where it has already covered 96%, how do i make it to 100%, I have Tried all the Best Pratices for the Test class, still i am not able to make it to 100%
This is my Helper Class
public class LeadHelper {
public static List<LeadShare> csShareList = new List<LeadShare>();
public static List<LeadShare> removeShareList = new List<LeadShare>();
public static List<Lead> leadsToAddAccountId = new List<Lead>();
public static List<String> leadCompanyNames = new List<String>();
public static Map<String, Id> matchingAcctNames = new Map<String,Id>();
public static void leadShareInsertUpdate(List<Lead> leads){
csShareList = new List<LeadShare>();
removeShareList = new List<LeadShare>();
Set<Id> ldIds = new Set<Id>();
for( Lead cs : leads) {
if( cs.CRM_User__c != NULL ) {
// Create a new LeadShare object for each Lead where CRM_User__c field is not NULL.
LeadShare csShare = new LeadShare();
// Give Read write access to that user for this particular Lead record.
csShare.LeadAccessLevel = 'edit';
// Assign Lead Id of Lead record.
csShare.LeadId = cs.id;
// Assign user id to grant read write access to this particular Lead record.
csShare.UserOrGroupId = cs.CRM_User__c;
csShareList.add( csShare );
}
if( cs.CRM_User__c == NULL ) {
ldIds.add( cs.id);
}
}
if(ldIds.size()>0){
removeShareList = [Select id,LeadId,UserOrGroupId from LeadShare where LeadId=:ldIds AND RowCause = 'Manual'];
if(removeShareList.size()>0){
delete removeShareList;
}
}
if( csShareList != null && csShareList.size() != 0 ) {
insert csShareList;
update csShareList;
}
}
public static void leadsToAddAccountId(List<Lead> leads){
leadsToAddAccountId = new List<Lead>();
leadCompanyNames = new List<String>();
matchingAcctNames = new Map<String,Id>();
for (Lead newLead : leads){
leadsToAddAccountId.add(newLead);
leadCompanyNames.add(newLead.Company);
}
for(Account acct : [Select Id, Name from Account where Name =: leadCompanyNames]){
matchingAcctNames.put(acct.Name, acct.Id);
}
for(Lead myLead : leadsToAddAccountId ){
myLead.Account__c = matchingAcctNames.get(myLead.Company);
}
}
public static void leadIncrementroundrobin(List<Lead> leads){
Id recordTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Lead Registration').getRecordTypeId();
List<Lead> leadList = [Select Id, Updated_Region__c, RecordTypeId From Lead where RecordTypeId=:recordTypeId ALL ROWS];
Map<String, List<Lead>> countryLeadsMap = new Map<String, List<Lead>>();
for (Lead l : leadList) {
if (!countryLeadsMap.containsKey(l.Updated_Region__c)) {
countryLeadsMap.put(l.Updated_Region__c, new List<Lead>());
}
countryLeadsMap.get(l.Updated_Region__c).add(l);
}
for (Lead nl : leads){
if(nl.RecordTypeId == recordTypeId){
if (!countryLeadsMap.containsKey(nl.Updated_Region__c)) {
countryLeadsMap.put(nl.Updated_Region__c, new List<Lead>());
}
countryLeadsMap.get(nl.Updated_Region__c).add(nl);
if (nl.Updated_Region__c == 'India') {
nl.Lead_No_Region_IN__c = countryLeadsMap.get(nl.Updated_Region__c).size();
}
if (nl.Updated_Region__c == 'US') {
nl.Lead_No_Region_USA__c = countryLeadsMap.get(nl.Updated_Region__c).size();
}
if (nl.Updated_Region__c == 'EU') {
nl.Lead_No_Region_EU__c = countryLeadsMap.get(nl.Updated_Region__c).size();
}
if (nl.Updated_Region__c == 'ME') {
nl.Lead_No_Region_ME__c = countryLeadsMap.get(nl.Updated_Region__c).size();
}
if (nl.Updated_Region__c == 'SEA1') {
nl.Lead_No_Region_SEA1__c = countryLeadsMap.get(nl.Updated_Region__c).size();
}
if (nl.Updated_Region__c == 'SEA2') {
nl.Lead_No_Region_SEA2__c = countryLeadsMap.get(nl.Updated_Region__c).size();
}
}
}
}
}
-----------------------------------
My test class
@isTest(seealldata=true)
private class LeadTrigger_Test {
// test that newly inserted records marked as pubic=true have corresponding shares created
public static testMethod void testAddShares() {
Set<ID> ids = new Set<ID>();
List<Lead> Leads = new List<Lead>();
Id RecordTypeIdLead = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Partner Lead Registration').getRecordTypeId();
User u = [Select id,name from user where isactive=true Limit 1];
for (Integer i=0;i<10;i++)
Leads.add(new Lead(FirstName='First ',LastName='Name '+i,RecordTypeId=RecordTypeIdLead,
Email='email'+i+'@email.com',Company='ABSYZ',CRM_User__c=u.Id));
insert Leads;
// get a set of all new created ids
for (Lead c : Leads)
ids.add(c.id);
// assert that 50 shares were created
List<LeadShare> shares = [select id from LeadShare where
LeadId IN :ids and RowCause = 'Manual'];
}
// insert records and switch them from public = true to public = false
public static testMethod void testUpdateContacts() {
Set<ID> ids = new Set<ID>();
List<Lead> Leads = new List<Lead>();
Id RecordTypeIdLead = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Partner Lead Registration').getRecordTypeId();
User u = [Select id,name from user where isactive=true Limit 1];
for (Integer i=0;i<50;i++)
Leads.add(new Lead(FirstName='First ',LastName='Name '+i,RecordTypeId=RecordTypeIdLead,
Email='email'+i+'@email.com',Company='ABSYZ',CRM_User__c=u.Id));
insert Leads;
for (Lead c : Leads)
ids.add(c.id);
update Leads;
// assert that 0 shares exist
List<LeadShare> shares = [select id from LeadShare where
LeadId IN :ids and RowCause = 'Manual'];
System.assertNotEquals(shares.size(),0);
for (Lead c : Leads)
c.CRM_User__c=u.Id;
update Leads;
// assert that 50 shares were created
shares = [select id from LeadShare where LeadId IN :ids and RowCause = 'Manual'];
System.assertEquals(shares.size(),50);
for (Lead c : Leads)
c.CRM_User__c=u.Id;
update Leads;
// assert that 0 shares exist
shares = [select id from LeadShare where LeadId IN :ids and RowCause = 'Manual'];
System.assertNotEquals(shares.size(),0);
}
public static testMethod void validateLeadAndAccount() {
List<Lead> leadList = new List<Lead>();
Id RecordTypeIdLead = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Lead Registration').getRecordTypeId();
Account acc = new Account();
Acc.Name = 'Test11';
insert acc;
Lead l1 = new Lead ();
l1.LastName = 'Test';
l1.RecordTypeId = RecordTypeIdLead;
l1.Company = 'Test11';
l1.Country = 'India';
l1.Lead_Category__c = 'Churned Customer';
l1.Status ='Enquiry';
leadList.add(l1);
Lead l2 = new Lead();
l2.Country = 'USA';
l2.Lead_Category__c = 'Churned Customer';
l2.Company = 'Test21';
l2.RecordTypeId = RecordTypeIdLead;
l2.LastName = 'testLast21';
l2.Status = 'Enquiry';
leadList.add(l2);
Lead l3 = new Lead();
l3.Country = 'Taiwan';
l3.Lead_Category__c = 'Churned Customer';
l3.Company = 'Test21';
l3.RecordTypeId = RecordTypeIdLead;
l3.LastName = 'testLast21';
l3.Status = 'Enquiry';
leadList.add(l3);
Lead l4 = new Lead();
l4.Country = 'Egypt';
l4.Lead_Category__c = 'Churned Customer';
l4.Company = 'Test21';
l4.RecordTypeId = RecordTypeIdLead;
l4.LastName = 'testLast21';
l4.Status = 'Enquiry';
leadList.add(l4);
Lead l5 = new Lead();
l5.Country = 'Germany';
l5.Lead_Category__c = 'Churned Customer';
l5.Company = 'Test21';
l5.RecordTypeId = RecordTypeIdLead;
l5.LastName = 'testLast21';
l5.Status = 'Enquiry';
leadList.add(l5);
Lead l6 = new Lead();
l6.Country = 'Malaysia';
l6.Lead_Category__c = 'Churned Customer';
l6.Company = 'Test21';
l6.RecordTypeId = RecordTypeIdLead;
l6.LastName = 'testLast21';
l6.Status = 'Enquiry';
leadList.add(l6);
Lead l7 = new Lead();
l7.Country = 'Singapore';
l7.Lead_Category__c = 'Churned Customer';
l7.Company = 'Test21';
l7.RecordTypeId = RecordTypeIdLead;
l7.LastName = 'testLast21';
l7.Status = 'Enquiry';
leadList.add(l7);
Test.startTest();
Insert leadList;
Test.stopTest();
}
}
- Janaki Rao
- February 12, 2020
- Like
- 0