function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
[Lindsey Kiken][Lindsey Kiken] 

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:
 
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?
Best Answer chosen by [Lindsey Kiken]
Shikha AgashiShikha Agashi
Please correct data for account as well as Contact. Insert some values for Licensing_Downloads__c whenever you insert as well as updating. Put different values while inserting and updating.

All Answers

Shikha AgashiShikha Agashi
Please correct data for account as well as Contact. Insert some values for Licensing_Downloads__c whenever you insert as well as updating. Put different values while inserting and updating.
This was selected as the best answer
Vijay NagarathinamVijay Nagarathinam
Hi Lindsey,

Please use the below code,
 
@IsTest
public class RelationshipTypesInsert_Test {

    public testMethod static void contactInsertion() {
		List<Account> updateAccount = new List<Account>();
		 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',Licensing_Downloads__c = 'Community');
        insert c;
		
		for(Account acc : [SELECT id,License_Downloads__c FROM Account where id =: acct.Id]){
			acc.License_Downloads__c = 'Salesforce';
			updateAccount.add(acc);
		}
		if(updateAccount.size() > 0){
			update updateAccount;
		}
	}
}

Thanks,
Vijay
[Lindsey Kiken][Lindsey Kiken]
@Shikha Agashi - That was excellent advice, thank you!

@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!