You need to sign in to do that
Don't have an account?
![[Lindsey Kiken] [Lindsey Kiken]](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
Trigger Only Receiving 27% Code Coverage via Test Class
I am fairly new to Apex and am having a very difficult time trying to pass my trigger beyond 27%.
My trigger is as follows:
My test class is as follows:
Anyone have any ideas?
My trigger is as follows:
trigger LicenseDownloads on Account (after update) { for(Account acc:Trigger.new){ Account oldAcc=Trigger.oldMap.get(acc.Id); if(oldAcc.License_Downloads__c!=acc.License_Downloads__c) { List<Contact>children=[SELECT Id,AccountId,Licensing_Downloads__c from Contact where AccountId = :acc.Id]; List<Contact>newids=new List<Contact>(); for(Contact con:children){ if(con.Licensing_Downloads__c!=acc.License_Downloads__c){ con.Licensing_Downloads__c=acc.License_Downloads__c; newids.add(con); } } if(newids.isEmpty()==false){ update newids; } } } }
My test class is as follows:
@IsTest public class RelationshipTypesInsert_Test { public testMethod static void contactInsertion() { Account acct = new Account(name='Apex Test Account',BillingState='Oregon',BillingCountry='United States'); insert acct; // Consider setting some address fields as required by copyAddressFields Contact c = new Contact(AccountId=acct.Id, lastname='testing', firstname='apex'); insert c; //Make some assertions based on the results of the copyAddressFields call List<Account> accountsToUpdate = new List<Account>(); for(Account a : [SELECT Id,Name,Technology_Partner_Tier__c,Account_Status__c,Channel_Partner__c FROM Account WHERE Name = 'Apex test Account']){ if(a.Account_Status__c == 'Inactive'){ a.Technology_Partner_Tier__c = 'Ecosystem'; a.Account_Status__c= 'Active'; a.Channel_Partner__c= 'Access'; /** Here put update actions **/ accountsToUpdate.add(a); } } update accountsToUpdate; } public testMethod static void contactInsertionFails() { Account acct = new Account(name='test account'); insert acct; // Set some fields that will cause an Exception in copyAddressFields Contact c = new Contact(AccountId=acct.Id, lastname='testing', firstname='apex', email='apex.testing@elementaltechonlogies.com'); insert c; //Make some assertions that errors were added to the Contact } public testMethod static void bulkifedInsertaion() { Account acct = new Account(name='test account',BillingState='Oregon',BillingCountry='United States',Technology_Partner_Tier__c='Ecosystem',Account_Status__c='Active',Channel_Partner__c='Access'); insert acct; Contact[] contactsToCreate = new Contact[]{}; for(Integer x=0; x<200;x++){ Contact ct = new Contact(AccountId=acct.Id,lastname='testing',firstname='apex',email='apex.testing'+x+'@elementaltechnologies.com'); contactsToCreate.add(ct); } Test.startTest(); insert contactsToCreate; Test.stopTest(); } }
Anyone have any ideas?
All Answers
Please use the below code,
Thanks,
Vijay
@Vijay Nagarathinam - Thank you for providing a new test class to test with. I unfortunately received an error at line 13 regarding the License_Downloads__c field and decided to instead focus on updating my test class based on Shikha's advice. Once updated, the triggers passed with 100% code coverage.
Thank you both for your help!