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
aam1raam1r 

Trigger Code Coverage 0%

Hi Guys, 

I know its another one, but i've looked at similar issues to see if i can identify the cause of my code coverage failure but have not found anything.  So, i'm getting 0% code coverage for a new trigger and cannot see why.  Here is my Trigger:
 
trigger MilestoneTaskTrigger on MPM4_BASE__Milestone1_Task__c (before insert, before update) {

if ((Trigger.isBefore && Trigger.isInsert) || (Trigger.isBefore && Trigger.isUpdate)){


	// Create list to hold Milestone Components to update
	List<Milestone_Component__c> components = new List<Milestone_Component__c>();

	// Get list of all Milestone Components that are set in Prod but not in Test
	List<Milestone_Component__c> cmps = [SELECT Id FROM Milestone_Component__c WHERE PROD__c = True AND Test_Sandbox__c = False];

	for (Milestone_Component__c c : cmps){
		c.Test_Sandbox__c = True;
		components.add(c);
	}

	for (MPM4_BASE__Milestone1_Task__c task : System.Trigger.new){
		// Is Refresh Test checked
		if (task.Refresh_Test__c == True && task.MPM4_BASE__Complete__c == True){
			// update all milestone components
			update components;

			// unset Resfresh Test checkbox
			task.Refresh_Test__c = False;
		}
	}
}

}

And here's my test class:
 
@isTest
public with sharing class MilestoneTaskTriggerTest {
	static testMethod void  MilestoneTaskTriggerTest() {
		
		// Insert Project
		System.debug('Inserting Milestone Project');
        MPM4_BASE__Milestone1_Project__c project = new MPM4_BASE__Milestone1_Project__c (Name='DEVELOPMENT');
        try { insert project; } catch (exception e) {}

        // get Salesforce Project record type for Milestone? - Nah!! default record type will be used.
        
        // Insert Milestone
        System.debug('Inserting Milestone');
        MPM4_BASE__Milestone1_Milestone__c milestone = new MPM4_BASE__Milestone1_Milestone__c(Name='Milestone', MPM4_BASE__Project__c=project.id);
        try { insert milestone; } catch (exception e) {}
		
		// Insert Task
		System.debug('Inserting Task');
        MPM4_BASE__Milestone1_Task__c task = new MPM4_BASE__Milestone1_Task__c(Name='Task', MPM4_BASE__Priority__c='4 - Low');
        try { insert task; } catch (exception e) {}

		// Insert Components (x3)
		System.debug('Inserting Components');
        Component__c component1 = new Component__c(Name='Comp1');
        try { insert component1; } catch (exception e) {}

        Component__c component2 = new Component__c(Name='Comp2');
        try { insert component2; } catch (exception e) {}

        Component__c component3 = new Component__c(Name='Comp3');
        try { insert component3; } catch (exception e) {}

        Component__c component4 = new Component__c(Name='Comp4');
        try { insert component4; } catch (exception e) {}

		// Insert Milestone Components (x3) setting Prod in one of them and 2 in test
		System.debug('Inserting Milestone Components');
        Milestone_Component__c mscomponent1 = new Milestone_Component__c(SFDC_Component__c=component1.id, Test_Sandbox__c=False, PROD__c=TRUE, Milestone__c=milestone.id, Task__c=task.id, Action__c='Update');
        try { insert mscomponent1; } catch (exception e) {}

        Milestone_Component__c mscomponent2 = new Milestone_Component__c(SFDC_Component__c=component2.id, Test_Sandbox__c=TRUE, PROD__c=False, Milestone__c=milestone.id, Task__c=task.id, Action__c='Update');
        try { insert mscomponent2; } catch (exception e) {}

        Milestone_Component__c mscomponent3 = new Milestone_Component__c(SFDC_Component__c=component3.id, Test_Sandbox__c=TRUE, PROD__c=False, Milestone__c=milestone.id, Task__c=task.id, Action__c='Update');
        try { insert mscomponent3; } catch (exception e) {}

        Milestone_Component__c mscomponent4 = new Milestone_Component__c(SFDC_Component__c=component4.id, Test_Sandbox__c=False, PROD__c=False, Milestone__c=milestone.id, Task__c=task.id, Action__c='Update');
        try { insert mscomponent4; } catch (exception e) {}

		// Update Task.Refresh_Test__c to true and set task to complete
		System.debug('Updating Task');
        task.Refresh_Test__c = TRUE;
        task.MPM4_BASE__Complete__c = TRUE;
        try { update task; } catch (exception e) {}

		// Check that there are 3 Milestone Components flagged as in Test
		List<Milestone_Component__c> mscomponents = [SELECT Id FROM Milestone_Component__c WHERE Test_Sandbox__c = TRUE];
		System.assertEquals(false, mscomponents.size()==3, ' there should be only 3 milestone components with Test Sandbox set as true');

		// Check that the Refresh_Test__c flag is false now.
		MPM4_BASE__Milestone1_Task__c checktask = [SELECT Id, Refresh_Test__c FROM MPM4_BASE__Milestone1_Task__c WHERE Id = :task.id];
		System.assertEquals(false, checktask.Refresh_Test__c==False, ' Refresh Test flag should be false!');

	}
}

Any help is greatly appreciated.
Thanks
Aamir
Ajay Nagar 7Ajay Nagar 7
Use Test.startTest(); and Test.stopTest(); also in test class method.
Naveen DhanarajNaveen Dhanaraj
Hi aam1r,
I have an idea ,you can write a class and call the class.method name in trigger.
 
aam1raam1r
Ajay, including those to create a test block also did not work.
Naveen - lets try out your suggestion.  However i'm still curious as to why the simple trigger with the test class i have coded does not work.  WIll update once i've tried out your method.
Ajay Nagar 7Ajay Nagar 7
Try after Removing "with sharing" from test class.
aam1raam1r
Ajay - Still no luck using the Test Start/Stop block along with removing the 'with sharing' bit. I must missing something obvious here!! 
Naveen - No silution either.  The trigger that invokes the handler class still fails to execute, thus giving 0% coverage.