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
Heather_HansonHeather_Hanson 

HELP! Need to boost code coverage from 74% to 75%

I created 1 new Trigger and a Test Class and it is saying that my overall code coverage is now 74%...but the test itself passes.

Maybe I'm missing something in my test?  Although I have a very similar trigger already and it is in production so not sure why this one is causing problems...

Here is the Trigger:
Trigger OppContactResi on Opportunity (before insert, before update) { 

set<Id> AccountIds = new set<Id>();
for (Opportunity  o: trigger.new) {
	AccountIds.add(o.AccountId);
}


map<Id, Account> AccountContactMap = new map<Id, Account>();
list<Account> AccConts = new list<Account>();
AccConts = [Select Id, Name, (Select Id, Name, Contact_Role__c from Contacts Where Contact_Role__c = 'MAIN CONTACT') from Account where Id IN : AccountIds];
for(Account acc : AccConts){
    AccountContactMap.put(acc.Id, acc);
}

for (Opportunity  o: trigger.new) {
       if(o.Contact__c == null && o.RecordTypeID == '0121H0000019Lna'){
			Account Acc = AccountContactMap.get(o.AccountId);
			if(Acc != null){
				List<Contact> conts = new list<Contact>(Acc.Contacts);
				if(conts.size() == 1){
					o.Contact__c = conts[0].Id;
				}
			}
		}
	}
}

Here is the TestClass:
 
@isTest 

public class OppContactResiTEST {
    
    static testMethod void createAccount()
	{

		Account a = new Account();
		a.Name = 'Test';
        a.RecordTypeId = '0121H0000019LnX';
        a.Language__c = 'ENGLISH';
		insert a;
		System.debug('created account');
			
		//Then create a non Main contact
		Contact c = new Contact();
        c.RecordtypeId = '0121H0000019LnZ';
		c.FirstName = 'Paul';
		c.LastName  = 'Test';
		c.AccountId = a.id;
		c.Contact_Role__c = 'BILLING CONTACT';
        c.Phone = '514-722-5656';
		insert c;
		System.debug('created signing contact');
			
		//Then create another Main contact
		Contact ci = new Contact();
		ci.FirstName = 'Bob';
		ci.LastName  = 'Test';
		ci.AccountId = a.id;
		ci.Contact_Role__c = 'MAIN CONTACT';
        ci.Phone = '514-722-5656';
		insert ci;
		System.debug('created non-signing contact');
        
        
        
        //Create Top Shelf Settings
        Scout__TopShelf_Settings__c ts = new Scout__TopShelf_Settings__c();
        ts.Scout__TopShelf_Username__c = 'selectcom';
        ts.Scout__TopShelf_Password__c = 'Fhtmdvp2';
        ts.Scout__My_Account__c = a.id;
        insert ts;
        System.debug('created top shelf settings');
		
       
		//Now create an opportunity
		Opportunity o = new Opportunity();
        o.AccountId = a.id;
        o.Name = 'Test -';
        o.Service_Agreement_Type__c = 'Residential Services';
		o.StageName = 'NEW - CALL IN';
		o.Type = 'SALES';
		o.CloseDate = Date.today();
        o.RecordTypeId = '0121H0000019Lna';
        o.Requested_Install_Time__c = '	In the morning (9:00am-12:00pm)';
        o.Internet_Requested__c = TRUE;
        o.Existing_Internet_Provider__c = 'Bell';
        o.Existing_Internet_Service__c = 'Yes';
        o.If_Bell_is_internet_on_line_or_Dry_Loop__c = 'Line';
        o.Language__c = 'ENGLISH';  
        o.Router_Option__c = 'Customer already has router';
		insert o;
		System.debug('created opportunity');
    		
        

	}
}

​​​​​​​
Best Answer chosen by Heather_Hanson
Andrew GAndrew G
Reading the code, it would appear this particular trigger has 100% "coverage" (use the term loosely).

I would be checking for other triggers or classes that have sub standard coverage and look to boost them.
Do a check using your favourite tool.  Mine is Sublime but the Eclipse IDE can do similar features.

Looking at the test code above I will say that whilst you are "covering" the lines, there are no asserts.  So how do you know that the code is working and doing what it is supposed to do?  How will you know that it continues to work if you change another trigger/class/PB?

I would add at a minimum something like the following:
List<opportunity> oTest = [SELECT id, contact__c FROM opportunity WHERE AccountId = :a.id LIMIT 1];
		List<contact> ciTest = [SELECT Id FROM Contact WHERE FirstName ='Bob' LIMIT 1];

		System.assertEquals(oTest[0].contact__c, ciTest[0].Id);

Note also that your trigger is an "insert or update" trigger and that your code only tests for an insert.  I would up the class to also cover the update.  I would also test for negative results.  No contact value set.

The Test class is that - a way to test your code.

Regards

Andrew



 

All Answers

Raj VakatiRaj Vakati
The reason i am thinking you hardcoded record type ID values .. 

Relace it in Trigger also 

try this

 
Id RecordTypeIdContact = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('DeveloperNameOfRecordType').getRecordTypeId();
 
@isTest 

public class OppContactResiTEST {
    
    static testMethod void createAccount()
	{
		
		Id recIdAcc = Schema.SObjectType.Account.getRecordTypeInfosByName().get('DeveloperNameOfRecordType').getRecordTypeId();
		Id recIdcon = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('DeveloperNameOfRecordType').getRecordTypeId();
		Id recIdopp = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('DeveloperNameOfRecordType').getRecordTypeId();


		Account a = new Account();
		a.Name = 'Test';
        a.RecordTypeId = recIdAcc;
        a.Language__c = 'ENGLISH';
		insert a;
		System.debug('created account');
			
		//Then create a non Main contact
		Contact c = new Contact();
        c.RecordtypeId = recIdcon;
		c.FirstName = 'Paul';
		c.LastName  = 'Test';
		c.AccountId = a.id;
		c.Contact_Role__c = 'BILLING CONTACT';
        c.Phone = '514-722-5656';
		insert c;
		System.debug('created signing contact');
			
		//Then create another Main contact
		Contact ci = new Contact();
		ci.FirstName = 'Bob';
		ci.LastName  = 'Test';
		ci.AccountId = a.id;
		ci.Contact_Role__c = 'MAIN CONTACT';
        ci.Phone = '514-722-5656';
		insert ci;
		System.debug('created non-signing contact');
        
        
        
        //Create Top Shelf Settings
        Scout__TopShelf_Settings__c ts = new Scout__TopShelf_Settings__c();
        ts.Scout__TopShelf_Username__c = 'selectcom';
        ts.Scout__TopShelf_Password__c = 'Fhtmdvp2';
        ts.Scout__My_Account__c = a.id;
        insert ts;
        System.debug('created top shelf settings');
		
       
		//Now create an opportunity
		Opportunity o = new Opportunity();
        o.AccountId = a.id;
        o.Name = 'Test -';
        o.Service_Agreement_Type__c = 'Residential Services';
		o.StageName = 'NEW - CALL IN';
		o.Type = 'SALES';
		o.CloseDate = Date.today();
        o.RecordTypeId = recIdopp;
        o.Requested_Install_Time__c = '	In the morning (9:00am-12:00pm)';
        o.Internet_Requested__c = TRUE;
        o.Existing_Internet_Provider__c = 'Bell';
        o.Existing_Internet_Service__c = 'Yes';
        o.If_Bell_is_internet_on_line_or_Dry_Loop__c = 'Line';
        o.Language__c = 'ENGLISH';  
        o.Router_Option__c = 'Customer already has router';
		insert o;
		System.debug('created opportunity');
    		
        

	}
}

 
Andrew GAndrew G
Reading the code, it would appear this particular trigger has 100% "coverage" (use the term loosely).

I would be checking for other triggers or classes that have sub standard coverage and look to boost them.
Do a check using your favourite tool.  Mine is Sublime but the Eclipse IDE can do similar features.

Looking at the test code above I will say that whilst you are "covering" the lines, there are no asserts.  So how do you know that the code is working and doing what it is supposed to do?  How will you know that it continues to work if you change another trigger/class/PB?

I would add at a minimum something like the following:
List<opportunity> oTest = [SELECT id, contact__c FROM opportunity WHERE AccountId = :a.id LIMIT 1];
		List<contact> ciTest = [SELECT Id FROM Contact WHERE FirstName ='Bob' LIMIT 1];

		System.assertEquals(oTest[0].contact__c, ciTest[0].Id);

Note also that your trigger is an "insert or update" trigger and that your code only tests for an insert.  I would up the class to also cover the update.  I would also test for negative results.  No contact value set.

The Test class is that - a way to test your code.

Regards

Andrew



 
This was selected as the best answer
Heather_HansonHeather_Hanson
Thanks for your help and for the advice Andrew since I am new to Apex Triggers (this is my second and it is very similar to my first!).

The issue was that a test was failing a lot on an installed package due to required fields not being fulfilled.  Luckily I was able to modify that class and correct the issue and then everything else sorted out.